#include <bits/stdc++.h>
using namespace std;
const int NMAX=1e5+2;
vector<int> adj[NMAX];
int dist[NMAX], s;
queue<int> q;
void bfs(int target)
{
dist[s]=0;
q.push(s);
while(!q.empty())
{
int current=q.front();
q.pop();
for(int i=0; i<adj[current].size(); i++)
{
int to=adj[current][i];
if(dist[to]==0 && to!=s)
{
q.push(to);
dist[to]=dist[current]+1;
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
freopen("bfs.in", "r", stdin);
freopen("bfs.out", "w", stdout);
int n, m; cin>>n>>m>>s;
for(int i=1; i<=m; i++)
{
int x, y; cin>>x>>y;
adj[x].push_back(y);
}
bfs(s);
for(int i=1; i<=n; i++)
{
if(dist[i]!=0 || i==s)cout<<dist[i]<<' ';
else cout<<"-1 ";
}
return 0;
}