#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector<vector<int>> graph;
vector<int> dist;
queue<int> q;
void BFS(int k) {
q.push(k);
dist[k] = 0;
while (!q.empty()) {
for (int next: graph[q.front()]) {
if (dist[next] == -1) {
q.push(next);
dist[next] = dist[q.front()] + 1;
}
}
q.pop();
}
}
int main() {
int N, M, S;
fin >> N >> M >> S;
graph.assign(N+1, vector<int>());
dist.assign(N+1, -1);
for (int i = 1 ; i <= M ; ++ i) {
int x, y;
fin >> x >> y;
graph[x].push_back(y);
}
BFS(S);
for (int i = 1 ; i <= N ; ++ i) {
fout << dist[i] << ' ';
}
fin.close();
fout.close();
}