Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n+1)
	d := make([]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}

		d[i+1] = -1
	}

	q := [][]int{}
	q = append(q, []int{1, 0})
	for len(q) > 0 {
		pop := q[0]
		if len(q) > 1 {
			q = q[1:]
		} else {
			q = [][]int{}
		}
		if d[pop[0]] >= 0 {
			continue
		}
		d[pop[0]] = pop[1]

		for j := 0; j < len(g[pop[0]]); j++ {
			q = append(q, []int{g[pop[0]][j], pop[1]+1})
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

C++: #include<iostream>
#include<queue>

using namespace std;
static const int R =100;
static const int IF = (1<<21);

int num,Mi[R][R],i,x,y;
int D[R];

void BF(int A){
  queue<int> Q;
  Q.push(A);
  for(i = 0;i<num;i++){
    D[i] = IF;
  }
  D[A] = 0;

  while(!Q.empty()){
    x= Q.front();
    Q.pop();

    for(y=0;y<num;y++){
      if(Mi[x][y] == 0){
	continue;
      }
      if(D[y] != IF){
	continue;
	  }
      D[y] = D[x] +1;
      Q.push(y);
    }
  }
  for(i =0; i<num; i++){
    cout << i+1 << " " <<((D[i] ==IF) ? (-1) : D[i]) << endl;
  }
}

  int main(){
    int x,z,y,i,j;

    cin >> num;

    for(i=0;i<num;i++){
      for(j=0;j<num;j++){
	Mi[i][j] = 0;
      }
    }

    for(i = 0;i<num;i++){
      cin >> x >>z;
      x--;

      for(j=0;j<z;j++){
	cin>>y;
	y--;
	Mi[x][y] =1;
      }
    }

    BF(0);

    return 0;
  }

C++: #include<iostream>
#include<queue>
using namespace std;

int main()
{
    queue<int> Q;
    int n,src=0;
    int a, b, c;
    cin >> n;
    int D[n], in[n][n];
    for (int i = 0; i < n; i++)
    {
        D[i] = -1;
        for (int j = 0; j < n; j++)
            in[i][j] = 0;
    }
    for (int i = 0; i < n; i++)
    {
        cin >> a >> b;
        for (int j = 0; j < b; j++)
        {
            cin >> c;
            in[c - 1][a - 1] = 1;
        }
    }
    Q.push(0);
    D[0] = 0;
    while (!Q.empty)
    {
        int cur = Q.front();
        Q.pop();
        for (int i = 0; i < n; i++)
        {
            if (in[i][cur] == 1 && D[i] == -1) {
                D[i] = D[cur] + 1;
                Q.push(i);
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << i + 1 << " " << D[i] << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)
#define lpin(a,i,n) for(int i=0;i<n;i++){cin>>a[i];}
int a[101][101],ans=-1,check[101],n;

void calc(int now,int g,int count){
  if(count>ans && ans!=-1) return;
  if(now==g){
    if(ans==-1){
      ans=count;
      return;
    }
    else{
      ans=min(ans,count);
      return;
    }
  }
  if(check[now]>count) check[now]=count;
  else return;
  lp(i,n+1){
    if(a[now][i]==1)
      calc(i,g,count+1);
  }
}

int main(){
  cin>>n;
  lp(i,n){
    int x,y;
    cin>>x>>y;
    lp(j,y){
      int z;
      cin>>z;
      a[x][z]=1;
    }
  }
  lp(i,n){
    lp(j,101){
      check[j]=1000;
    }
    ans=-1;
    calc(1,i+1,0);
    cout<<i+1<<" "<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
#define Lens 105
using namespace std;
int N;
int A[Lens][Lens];
int d[Lens];
char c[Lens];
queue<int> Q;

int main()
{
	cin >> N;
	Q.push(1);
	for (int i = 1; i <= N; i++)
	{
		int k, n;
		cin >> k >> n;
		c[k] = 'w';
		for (int i = 0; i < n; i++)
		{
			int j;
			cin >> j;
			A[k][j] = 1;
		}
	}
	while (Q.size())
	{
		int i = Q.front();
		Q.pop();
		for(int j=1;j<=N;j++)
		{
			if (A[i][j] == 1 && c[j] == 'w')
			{
				c[j] = 'g';
				d[j] = d[i] + 1;
				Q.push(j);
			}	 
		}
	}
	for (int i = 2; i <= N; i++)
	{
		if (c[i] == 'w')
			d[i] = -1;
	}	
	for (int i = 1; i <= N; i++)
	{
		cout << i << ' ' << d[i] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;

const int INF = 1 << 10;
const int MAX_N = 110;
const int DONE = 2;
const int TOUCHED = 1;
const int NOT_TOUCHED = 0;
int G[MAX_N][MAX_N];
int D[MAX_N];
int V[110];

int main() {
  int n, i, j, u, k, v, d;
  queue<int> q;

  cin >> n;
  for (i = 0; i < n; ++i) {
    D[i] = INF;
  }

  for (i = 0; i < n; ++i) {
    cin >> u >> k;
    for (j = 0; j < k; ++j) {
      cin >> v;
      G[u - 1][v - 1] = 1;
    }
  }

  D[0] = 0;
  V[0] = TOUCHED;
  q.push(0);
  while (!q.empty()) {
    i = q.front(); q.pop();
    for (j = 0; j < n; ++j) {
      if (G[i][j] && V[j] != DONE) {
        if (V[j] == NOT_TOUCHED) {
          V[j] = TOUCHED;
          q.push(j);
        }
        D[j] = min(D[j], D[i] + 1);
      }
    }
    V[i] = DONE;
  }

  for (i = 0; i < n; ++i) {
    d = D[i] == INF ? -1 : D[i];
    cout << i + 1 << ' ' << d << '\n';
  }

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

using i64 = int64_t;

using Graph = vector<vector<int>>;

int main(){
    int n;
    cin >> n;
    Graph graph(n+1, vector<int>());
    for(int i=0;i<n;++i){
        int u, k;
        cin >> u >> k;
        for(int j=0;j<k;++j){
            int v;
            cin >> v;
            graph[u].push_back(v);

        }
    }

    vector<int> mini(n+1, 200);
    queue<pair<int, int>> que;
    mini[1] = 0;
    que.push({1, 0});
    while(!que.empty()){
        auto cur = que.front(); que.pop();
        for(auto to: graph[cur.first]){
            int cost = cur.second + 1;
            if(mini[to] <= cost)continue;
            mini[to] = cost;
            que.push({to, cost});
        }
    }

    for(int i=1;i<=n;++i){
        cout << i << " " << (mini[i] < 200 ? mini[i] : -1) << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using Graph = vector<vector<int>>;
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
const int inf = 1e8;

int main() {
    int n;
    cin >> n;
    Graph g(n);
    rep(i,n) {
        int id,k;
        cin >> id >> k;
        id--;
        rep(j,k) {
            int v;
            cin >> v;
            v--;
            g[id].push_back(v);
        }
    }
    vector<int> d(n);
    rep(i,n) d[i] = inf;
    d[0] = 0;
    queue<int> q;
    q.push(0);
    while (!q.empty()) {
        int v = q.front(); q.pop();
        for (int n_v: g[v]) if (chmin(d[n_v],d[v]+1)) q.push(n_v);       
    };
    rep(i,n) if (d[i] == inf) d[i] = -1;
    rep(i,n) cout << i+1 << " " << d[i] << endl;
}

C++: #include<cstdio>
#include<queue>
#define MAX 100
using namespace std;
int n, g[MAX][MAX], labels[MAX], d[MAX];
queue<int> Q;

void bfs(int s) {
    int v,u;
    labels[s] = 1;
    d[s]=0;
    Q.push(s);
    while (!Q.empty()) {
	u = Q.front(); Q.pop();
	for (v=0; v<n; v++) {
	    if (g[u][v] && labels[v]==0) {
		labels[v] =1;
		d[v] = d[u] +1;
		Q.push(v);
	    }
	}
	labels[u] = 2;
    }
}

int main() {
    int i,u,k,v,j;
    scanf("%d",&n);
    for(i=0;i<n;i++){
	for(j=0;j<n;j++){
	    g[i][j]=0;
	}
    }

    for(i=0;i<n;i++){
	scanf("%d %d",&u, &k);
	for(j=0;j<k;j++){
	    scanf("%d",&v);
	    g[u-1][v-1]=1;
	}
    }

    for(i=0; i<n; i++) { labels[i]=0; d[i]=0;}

    bfs(0);

    for(i=0; i<n; i++) printf("%d %d\n", i+1, d[i]);

    return 0;
}