#include <iostream>
#include <fstream>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
int cost[100002];
queue < int > q;
vector < int > v[100002];
int main()
{
ifstream f("bfs.in");
ofstream g("bfs.out");
int n,m,s;
f>>n>>m>>s;
for(int i=1;i<=m;i++)
{
int x,y; f>>x>>y;
v[x].push_back(y);
}
memset(cost,-1,sizeof(cost));
cost[s]=0;
q.push(s);
while(!q.empty())
{
int vf=q.front();
q.pop();
for(int i=0;i<v[vf].size();i++)
if(cost[v[vf][i]]==-1)
{
q.push(v[vf][i]);
cost[v[vf][i]]=cost[vf]+1;
}
}
for(int i=1;i<=n;i++)
g<<cost[i]<<" ";
g<<'\n';
f.close();
g.close();
return 0;
}