#include <bits/stdc++.h>
using namespace std;
#define NMAX 100001
ifstream fin( "bfs.in" );
ofstream fout( "bfs.out" );
vector <int> edges[NMAX];
queue <int> q;
int dist[NMAX];
void bfs( int s ) {
dist[s] = 1;
q.push(s);
while( !q.empty() ) {
int aux = q.back();
q.pop();
for( auto i : edges[aux] ) {
if( !dist[i] ) {
dist[i] = dist[aux] + 1;
q.push(i);
}
}
}
}
int main() {
int n, m, a, b, i, sursa;
fin >> n >> m >> sursa;
for( i = 0; i < m; i++ ) {
fin >> a >> b;
edges[a].push_back(b);
}
bfs( sursa );
for( i = 0; i < n; i++ )
fout << dist[i+1] - 1 << " ";
return 0;
}