#include <bits/stdc++.h>
#define start ios_base::sync_with_stdio(false); fin.tie(0); fout.tie(0);
#define stop fin.close(); fout.close(); return 0;
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
const int VMAX = 100005;
int n, m, s;
bool viz[VMAX];
vector < int > graf[VMAX], distanta(VMAX, -1);
queue < int > coada;
void BFS()
{
int nod;
while (!coada.empty())
{
nod = coada.front();
coada.pop();
for (int i = 0; i < graf[nod].size(); ++i)
{
int nextnode = graf[nod][i];
if (distanta[nextnode] == -1)
distanta[nextnode] = distanta[nod] + 1,
coada.push(nextnode);
}
}
}
int main()
{
start
int x, y;
fin >> n >> m >> s;
distanta[s] = 0;
while (m--)
{
fin >> x >> y;
graf[x].push_back(y);
}
coada.push(s);
BFS();
for (int i = 1; i <= n; ++i)
fout << distanta[i] << " ";
stop
}