Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 0
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  int n;
  cin >> n;
  vector<vi> g(n);
  rep(i, n) {
    int u, k;
    cin >> u >> k;
    rep(j, k) {
      int u;
      cin >> u;
      u--;
      g[i].push_back(u);
    }
  }
  vi ans(n, -1);
  ans[0] = 0;
  queue<int> q;
  q.push(0);
  while(!q.empty()) {
    int i = q.front();
    q.pop();
    for(int v : g[i]) {
      if(ans[v] != -1) continue;
      ans[v] = ans[i] + 1;
      q.push(v);
    }
  }
  rep(i, n) cout << i + 1 << ' ' << ans[i] << endl;
}

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;
}