Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ds = append(ds, -1)
	}

	q := make([]int, 0, n)
	q = append(q, 0)
	ds[0] = 0

	for len(q) > 0 {
		i := pop(&q)
		for _, v := range vecs[i] {
			if ds[v] == -1 {
				ds[v] = ds[i]+1
				q = append(q, v)
			}
		}
	}

	for i, d := range ds {
		fmt.Fprintln(w, i+1, d)
	}

	w.Flush()
}

func pop(q *[]int) int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

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

int main(){
  int n;
  cin>>n;
  int a[110][110] = {};
  for(int i=1;i<=n;i++){
    int u,k;
    cin>>u>>k;
    for(int j=0;j<k;j++){
      int v;
      cin>>v;
      a[i][v]=1;
    }
  }

  int d[110];
  for (int i=1;i<=n;i++){
    d[i]=-1;
  }
  queue<int> q;
  d[1]=0;
  q.push(1);
  while(! q.empty()){
    int cur =q.front();
    q.pop();
    for(int i=1;i<=n;i++){
      if(a[cur][i]==1 && d[i]==-1){
	d[i]=d[cur]+1;
	q.push(i);
      }
    }
  }
  for(int i=1;i<=n;i++){
    cout<<i<<" "<<d[i]<<endl;
  }
}

C++: #include <cstdio>
#include <cstring>
#include <queue>
#define N 110
#define INF 0x3f3f3f3f
using namespace std;
//stack<int> s;
int n;
int m[N][N];
int used[N];
int d[N];
void bfs(int root){
    queue<int>q;
    while(!q.empty()) q.pop();
    q.push(root);
    d[root] = 0;
    while(!q.empty()){
        int u = q.front();
        used[u] = 1;
        q.pop();
        for(int i = 1; i <= n; i++){
            if(used[i] == 0 && m[u][i]){
                q.push(i);
                d[i] = d[u] + 1;
                used[i] = 1;
            }
        }
    }
}
int main(){
    for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) m[i][j] = 0;
    for(int i = 0; i < N; i++) used[i] = 0;
    for(int i = 0; i < N; i++) d[i] = INF;
    scanf("%d", &n);
    for(int p = 0; p < n; p++){
        int node, k;
        scanf("%d %d", &node, &k);
        for(int i = 0; i < k; i++){
            int b;
            scanf("%d", &b);
            m[node][b] = 1;
        }
    }
    for(int i = 1; i <= n; i++) if(used[i] == 0) bfs(i);
    for(int i = 1; i <= n; i++){
        printf("%d %d\n", i, d[i]);
    }
    return 0;
}

C++: #include <stdio.h>
#include <string.h>
#include<iostream>
#include<queue>
using namespace std;

char s[13][1];
int D[106][106];
int A[106],f[106],w,h,n,sx,sy,gx,gy,e,g[106];
int   kensa(int ,int);
int main(){
	queue<int> P;
	int i,j,a,b,c,d;
	for(i=1;i<=100;i++){
		for(j=1;j<=100;j++){
			D[i][j]=-1;		
		}					
	}						
	for(i=1;i<=100;i++){		
		A[i]=0;				
		f[i]=-1;
		g[i]=0;
	}							
	scanf("%d" ,&a);//     a=4
	for(i=1;i<=a;i++){
        scanf("%d",&b);		//
		scanf("%d",&b);			//b=2					//
		for(j=1;j<=b;j++){		//
		scanf("%d",&c);			//					//
		D[i][c]=1;
//
		}						//D[???][???]
	}				
	for(i=1;i<=a;i++){
		for(j=1;j<=a;j++){
	//		printf("%d %d %d\n",i,j,D[i][j]);
		}	
	}//
	P.push(1);
	//P???1
	f[1]=0;
	while(!P.empty()){			//
		d=P.front();
		//
		A[d]=1;
		//
		P.pop();				//
		for(i=1;i<=a;i++){
			//printf("%d %d %d\n",d,i,D[d][i]);//
			if(D[d][i]==-1){		//
			continue;			//
			}					//
			if(A[i]==1){		//
			continue;			//
			}					//
			P.push(i);			//
			
			g[i]=g[d]+1;
			f[i]=g[d]+1;
			A[i]=1;
			//printf("%d\n",P.back());
		}	
	}							//
	for(i=1;i<=a;i++){			//
	printf("%d %d\n",i, f[i]);	//
	}
	return 0;
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  for ( int i = 0; i < n; i++ ) d[i] = INFTY;
  d[s] = 0;
  int u;
  while ( !q.empty() ) {
    u = q.front(); q.pop();
    for ( int v = 0; v < n; v++ ) {
      if ( M[u][v] == 0 ) continue;
      if ( d[v] != INFTY ) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for ( int i = 0; i < n; i++ ) {
    cout << i + 1 << " " << ( ( d[i] == INFTY ) ? ( -1 ) : d[i] ) << endl;
  }
}

int main() {
  int u, k, v;

  cin >> n;
  for ( int i = 0; i < n; i++ ) {
    for ( int j = 0; j < n; j++ ) M[i][j] = 0;
  }

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

  bfs(0);

  return 0;
}

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

#define MAX 100
int n;
int M[MAX][MAX];
int dist[MAX];

void bfs(int);

int main(){
  int u,k,v;
  int i,j;
  cin >> n;
  for(i = 0; i < n; i++){
    for(j = 0; j < n; j++){
      M[i][j] = 0;
    }
  }
  for(i = 0; i < n; i++){
    cin >> u;
    cin >> k;
    u--;
    for( i = 0; i < n; i++){
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);
  return 0;
}

void bfs(int s){
  queue<int> q;
  int u,v;
  int i;
  q.push(s);
  for(i = 0; i < n; i++){
    dist[i] = INFINITY;
  }
  dist[s] = 0;
  while( !q.empty() ){
    u = q.front();
    q.pop();
    //for(i = 0; i < n; i++){
      for(v = 0; v < n; v++){
        if(M[u][v] == 0){
          continue;
        }
        if( dist[v] != INFINITY){
          continue;
        }
        dist[v] = dist[u] + 1;
        q.push(v);
      }
  }
  for(i = 0; i < n; i++){
    cout << i+1 << " " << ( (dist[i] == INFINITY) ? (-1) : dist[i] ) << endl;
    //if( d[i] == INFINITY)
  }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#define eol '\n'
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  
  queue<int> que;
  vector< vector<int> > list(n+1);
  vector<int> d(n+1, -1);

  for (int i = 0; i < n; i++) {
    int u,k;
    cin >> u >> k;
    
    vector<int> row(k);
    for (int j = 0; j < k; j++) {
      cin >> row[j];
    }
    list[u] = row;
  }

  d[1] = 0;
  que.push(1);
  while(que.size()) {
    int v = que.front();
    que.pop();

    int dis = d[v];

    for (int i = 0; i < list[v].size(); i++) {
      int next = list[v][i];
      if (d[next] == -1) {
        d[next] = dis+1;
        que.push(next);
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    cout << i << " " << d[i] << eol;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int N = 100;
const int INF = 1<<21;
int n, M[N][N];
int d[N];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  rep(i, n) d[i] = INF;
  d[s] = 0;
  int u;
  while(!q.empty()) {
    u = q.front();
    q.pop();
    rep(v, n) {
      if (!M[u][v]) continue;
      if (d[v] != INF) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  rep(i, n) {
    cout << i+1 << " " << ((d[i] == INF) ? (-1) : d[i]) << endl;
  }
}

int main() {
  int u, k, v;
  cin >> n;
  rep(i, n) {
    rep(j, n) {
      M[i][j] = 0;
    }
  }
  rep(i, n) {
    cin >> u >> k;
    u--;
    rep(j, k) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);
  
  return 0;
}