#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector <int> v[100005];
deque <int> c;
bool w[100005];
int d[100005];
int main()
{
int n, m, i, s, nr=0, x, y;
fin >> n >> m >> s;
for(i=1; i<=m; i++)
{
fin >> x >> y;
v[x].push_back(y);
}
c.push_back(s);
w[s]=1;
while(c.empty()==0)
{
int aux=c[0];
for(i=0; i<v[aux].size(); i++)
{
if(w[v[aux][i]]==0)
{
d[v[aux][i]]=d[aux]+1;
c.push_back(v[aux][i]);
//fout << v[aux][i] ,, " "
w[v[aux][i]]=1;
}
}
c.pop_front();
}
for(i=1; i<=n; i++)
{
if(d[i]==0 && i!=s) fout << -1 << " ";
else fout << d[i] << " ";
}
return 0;
}