#include <fstream>
#include <vector>
#include <queue>
#define NMAX 100005
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector<int> G[NMAX];
queue<int> Q;
int inC[NMAX];
int N,M,S;
void BFS(int x);
int main()
{
int a,b;
fin>>N>>M>>S;
for(int i = 1; i<=M; i++)
{
fin>>a>>b;
G[a].push_back(b);
}
BFS(S);
for(int i = 1; i<=N; i++)
fout<<inC[i]-1<<" ";
fout<<'\n';
return 0;
}
void BFS(int x)
{
Q.push(x);
inC[x] = 1;
while(!Q.empty())
{
int nod = Q.front();
Q.pop();
for(int i = 0; i<G[nod].size(); i++)
if(!inC[G[nod][i]])
{
Q.push(G[nod][i]);
inC[G[nod][i]] = inC[nod]+1;
}
}
}