#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
const int NMAX = 1e5;
const int INF = 1e9;
vector<int> L[NMAX + 1];
int dist[NMAX + 1];
void bfs(int source)
{
for (int i = 1; i <= NMAX; ++i) {
dist[i] = INF;
}
dist[source] = 0;
queue<int> q;
q.push(source);
while (!q.empty()) {
int current = q.front();
q.pop();
for (int neighbor : L[current]) {
if (dist[neighbor] == INF) {
dist[neighbor] = dist[current] + 1;
q.push(neighbor);
}
}
}
}
int main()
{
int n, m, s;
fin >> n >> m >> s;
for (int i = 0; i < m; ++i) {
int u, v;
fin >> u >> v;
L[u].push_back(v);
}
bfs(s);
for (int i = 1; i <= n; ++i) {
if (dist[i] == INF) {
fout << -1 << " ";
} else {
fout << dist[i] << " ";
}
}
fout << '\n';
return 0;
}