#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
vector <int> G[100005];
queue <int> q;
int viz[100005];
void bfs(int u)
{
int v,l;
viz[u]=0;
q.push(u);
while(!q.empty())
{
u=q.front();
q.pop();
l=G[u].size();
for(int i=0; i<l; i++)
{
v=G[u][i];
if(viz[v]==-1)
{
viz[v]=viz[u]+1;
q.push(v);
}
}
}
}
int main()
{
int n,m,s,x,y;
in>>n>>m>>s;
for(int i=1; i<=m; i++)
{
in>>x>>y;
G[x].push_back(y);
}
for(int i=1; i<=n; i++)
viz[i]=-1;
bfs(s);
for(int i=1; i<=n; i++)
out<<viz[i]<<" ";
return 0;
}