#include <fstream>
#include <queue>
#include <vector>
std::ifstream f ("bfs.in");
std::ofstream g ("bfs.out");
int main() {
int n, m, s;
f >> n >> m >> s;
std::vector<std::vector<int>> adj(n+1);
for (int i = 1; i <= m; i++) {
int x, y;
f >> x >> y;
adj[x].push_back(y);
}
std::vector<int> dist(n+1, -1);
std::queue<int> q;
q.push(s); dist[s] = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i : adj[x]) {
if (dist[i] == -1) {
dist[i] = dist[x] + 1;
q.push(i);
}
}
}
for (int i = 1; i <= n; i++) {
g << dist[i] << " ";
}
return 0;
}