#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <bitset>
#include <cmath>
#include <queue>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int N, M, S, cost[100005];
vector < int > G[100005];
queue < int > Q;
void bfs(int sursa)
{
memset(cost, -1, sizeof(cost));
Q.push(sursa); cost[sursa]=0;
while (Q.size())
{
int nod=Q.front(), sz=G[nod].size(); Q.pop();
for (int i=0; i<sz; ++i)
{
int aux=G[nod][i];
if (cost[aux]==-1)
cost[aux]=cost[nod]+1, Q.push(aux);
}
}
}
int main()
{
f>>N>>M>>S;
for (int i=1; i<=M; ++i)
{
int x, y; f>>x>>y;
G[x].push_back(y);
}
bfs(S);
for (int i=1; i<=N; ++i)
g<<cost[i]<<' '; g<<'\n';
return 0;
}