#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
vector<int> L[100001];
vector<int> dist(100001);
int n, m, s;
void bfs(int s){
fill(dist.begin(), dist.end(), -1);
dist[s] = 0;
queue<int> q;
q.push(s);
while(q.empty() == false){
int node = q.front();
q.pop();
for(auto vec : L[node]){
if(dist[vec] == -1){
dist[vec] = dist[node] + 1;
q.push(vec);
}
}
}
}
int main() {
ifstream fin("dfs.in");
ofstream fout("dfs.out");
fin >> n >> m >> s;
for (int i{1}; i <= m; i++) {
int a, b;
fin >> a >> b;
L[a].push_back(b);
// L[b].push_back(a);
}
bfs(s);
for(int i{1}; i <= n; i++) cout << dist[i] << ' ';
}