#include<iostream>
#include<fstream>
#include<list>
#include<queue>
#include<bitset>
using namespace std;
const int PCT=100000;
list<int>gr[PCT];
queue<int> q;
queue<int> l;
int b[PCT];
void bfs(int s)
{
list<int>::iterator i;
q.push(s);
l.push(0);
int lung;
b[s]=-1;
while(q.size())
{
s=q.front();
q.pop();
lung=l.front();
l.pop();
for(i=gr[s].begin();i!=gr[s].end();++i)
{
if(b[*i]==0)
{
q.push(*i);
l.push(lung+1);
b[*i]=lung+1;
}
}
}
}
int main()
{
//ifstream si;
//si.open("bfs.in");
FILE*si=fopen("bfs.in","r");
ofstream so;
so.open("bfs.out");
int n,m,s;
fscanf(si,"%i %i %i",&n,&m,&s);
--s;
int i,a,c;
for(i=0;i<m;++i)
{
fscanf(si,"%i %i",&a,&c);
gr[a-1].push_back(c-1);
}
bfs(s);
for(i=0;i<n;++i)
{
if(b[i]==0)
{
so<<-1<<' ';
}
else
{
if(b[i]==-1)
so<<0<<' ';
else
so<<b[i]<<' ';
}
}
so<<'\n';
return 0;
}