#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
vector <int> g[100001];
queue <int> q;
int sol[100001], S;
bool sel[100001];
void bfs (void)
{
int i, nod, y;
q.push(S); sel[S]=true; sol[S]=0;
while (!q.empty())
{
nod=q.front();
q.pop();
for (i=0; i<g[nod].size(); i++)
{
y=g[nod][i];
if (!sel[y])
{
sol[y]=sol[nod]+1;
q.push(y);
sel[y]=false;
}
}
}
}
int main()
{
int n, m, i, x, y;
freopen("bfs.in","r",stdin);
freopen("bfs.out","w",stdout);
scanf("%d%d%d",&n,&m,&S);
for (i=1; i<=m; i++)
{
scanf("%d%d",&x,&y);
g[x].push_back(y);
}
for (i=1; i<=n; i++) sol[i]=-1;
bfs();
for (i=1; i<=n; i++) printf("%d ",sol[i]);
return 0;
}