Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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

using namespace std;

const int INF = 100000000;

int main(){
	int n;
	cin >> n;
	vector<vector<int>> node(n+1);
	for(int i=0;i<n;i++){
		int u,k,tmp;
		cin >> u >> k;
		for(int j=0;j<k;j++){
			cin >> tmp;
			node[u].push_back(tmp);
		}
	}
	
	//search
	vector<int> res(n+1,INF);
	queue<int> que;
	queue<int> copied;
	queue<int> zeros;
	que.push(1);
	
	int count = 0;
	for(int i=0;i<n-1;i++){
		while(que.size()){
			int k = que.front(); que.pop();
			//cout << "-> " << k << endl;
			if(res[k] >= INF) res[k] = count;
			for(int v:node[k]){
				copied.push(v);
			}
		}
		count++;
		que = copied;
		copied = zeros;
	}

	//output
	for(int i=1;i<=n;i++){
		cout << i << " " << res[i] << endl;
	}

	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <stack>
#include <queue>
#include <list>
#include <utility>
#include <set>
using namespace std;

int main(){
    int n;
    cin >> n;
    int g[110][110];
    int i, j;
    int u, k, v;
    int p;
    int dist[110];
    bool visited[110];
    
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            g[i][j] = 0;
        }
    }
    
    for(i = 0; i < n; i++){
        dist[i] = -1;
        visited[i] = false;
    }
    
    for(i = 0; i < n; i++){
        cin >> u >> k;
        u--;
        for(j = 0; j < k; j++){
            cin >> v;
            v--;
            g[u][v] = 1;
        }
    }
    
    queue<int> q1, q2;
    q1.push(0);
    
    for(i = 0; q1.size(); i++){
        while(!q1.empty()){
            p = q1.front();
            q1.pop();
            
            if(visited[p]) continue;
            visited[p] = true;
            
            dist[p] = i;
            
            for(j = 0; j < n; j++){
                if(g[p][j]){
                    q2.push(j);
                }
            }
        }
        q1 = q2;
        while(!q2.empty()){
            q2.pop();
        }
    }
    
    for(i = 0; i < n; i++){
        printf("%d %d\n", i+1, dist[i]);
    }
}

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

int n, u, k, v, dp[101];
bool li[101][101];


int main(){
	fill(dp, dp + 101, -1);
	for (int i = 0; i < 101; i++) {
		fill(li[i], li[i] + 101, false);
	}
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v;
			li[u][v] = li[v][u] = true;
		}
	}
	vector<int> l = vector<int>(1, 1);
	vector<int> l2 = vector<int>();
	dp[1] = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < l.size(); j++) {
			for (int k = 1; k <= n; k++) {
				if (li[l[j]][k] && dp[k] < 0) {
					dp[k] = i;
					l2.push_back(k);
				}
			}
		}
		l2.swap(l);
		l2.swap(vector<int>());
	}
	for (int i = 1; i <= n; i++) {
		cout << i << " " << dp[i] << endl;
	}

	return 0;
}

C++: #include <iostream>

using namespace std;

#define N_MAX 100

int Adj[N_MAX][N_MAX] = {};
int Min_d[N_MAX];

void bfs(int v, int d, int num_node);

int main()
{
    int n, i, u, k, v, j;

    cin >> n;

    for (i = 0; i < n; i++) Min_d[i] = -1;

    //??£??\???????????????
    for (i = 0; i < n; i++) {
        cin >> u >> k;
        while (k-- > 0) {
            cin >> v;
            Adj[u-1][v-1] = 1;
        }
    }
/*
    cout << "show Adjacency matrix" << endl;
    for (i = 0; i < n; i++) {
        for (j = 0; j < n ; j++) {
            if (j == n - 1)
                cout << Adj[i][j] << endl;
            else
                cout << Adj[i][j] << ' ';
        }
    }
*/
    //??¢?´¢??????
    Min_d[0] = 0;
    bfs(1, 1, n);

    //????????????
    for(i = 1; i <= n; i++) {
        cout << i << ' ' << Min_d[i-1] << endl;
    }

    return 0;
}

void bfs(int v, int d, int num_node) {
    int i;
//    cout << "bfs node : " << v << endl;

    for(i = 1; i <= num_node; i++) {
        if (Adj[v-1][i-1] == 1) {
            if (Min_d[i-1] == -1 || Min_d[i-1] > d)
                Min_d[i-1] = d;
            bfs(i, d+1, num_node);
        }
    }
}

C++: #include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int n, d[101]; vector<int> v[101];
void bfs(int c, int x) {
  queue<int> q; q.push(c);
  d[c] = x;
  while(!q.empty()) {
    int t = q.front(); q.pop();
    for(int i = 0; i < v[t].size(); ++i) {
      if(d[v[t][i]] > -1) continue;
      q.push(v[t][i]);
      d[v[t][i]] = d[t]+1;
    }
  }
}
int main() {
  scanf("%d", &n);
  for(int i = 0, u, k; i < n; ++i) {
    scanf("%d %d", &u, &k);
    for(int j = 0, vj; j < k; ++j) {
      scanf("%d", &vj);
      v[u].push_back(vj);
    }
    d[i+1] = -1;
  }
  bfs(1, 0);
  for(int id = 1; id <= n; ++id) {
    printf("%d %d\n", id, d[id]);
  }
}

C++: #include<stdio.h>
#define MAX 100
#define INFTY (1 << 21)
#define White 0
#define Gray 1
#define Black 2
void bfs(void);
void enqueue(int);
int dequeue();
int isEmpty();
int n,M[MAX][MAX];
int head=0,tail=0,Q[MAX];
int main(){
  int u,k,v;
  int i,j;
  scanf("%d",&n);
  for(i = 0; i < n; i++){
    for(j = 0; j < n; j++){
      M[i][j] = 0;
    }
  }

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

  return 0;
}

void bfs(){
  int color[MAX];
  int d[MAX];
  int i,v;
  for(i = 0; i < n; i++){
    color[i] = White;
    d[i] = INFTY;
  }
  color[0] = Gray;
  d[0] = 0;
  enqueue(0);

  while(!isEmpty()){
    int u;
    u = dequeue();
    for(v = 0; v < n; v++){
      if(M[u][v] && color[v] == White){
        color[v] = Gray;
        d[v] = d[u] + 1;
        enqueue(v);
      }
    }
    color[u] = Black;
  }
  for(i = 0; i < n; i++) printf("%d %d\n",i+1,d[i]);
}

void enqueue(int x){
  Q[tail] = x;
  if(tail + 1 == MAX)tail = 0;
  else tail++;
}

int dequeue(){
  int x;
  x = Q[head];
  if(head + 1 == MAX) head = 0;
  else head++;
  return x;
}

 int isEmpty(){
   if(head == tail) return 1;
   else return 0;
 }