#include<iostream>
#include<fstream>
#include<list>
#include<queue>
#include<bitset>
using namespace std;
const int PCT=100000;
list<int>gr[PCT];
struct punct
{
int q;
int l;
};
queue<punct> p;
int b[PCT];
bitset<PCT> viz;
void bfs(int s)
{
list<int>::iterator i;
p.push({s,0});
int lung;
viz[s]=1;
punct str;
while(p.size())
{
str=p.front();
p.pop();
for(i=gr[str.q].begin();i!=gr[str.q].end();++i)
{
if(viz[*i]==0)
{
b[*i]=str.l+1;
p.push({*i,b[*i]});
viz[*i]=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(!viz[i])
{
so<<-1<<' ';
}
else
{
so<<b[i]<<' ';
}
}
so<<'\n';
return 0;
}