Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

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

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

C++: #include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
bool vis[107];
int G[107][107], dist[107];
queue<int> q;
int main() {
    int n;
    scanf("%d", &n);
    memset(dist, -1, sizeof(dist));
    for (int i = 1; i <= n; i++) {
    	int u, k;
    	scanf("%d%d", &u, &k);
    	for (int j = 0; j < k; j++) {
    		scanf("%d", &G[u][j]);
		}
	}
	dist[1] = 0;
	q.push(1);
	vis[1] = true;
	while (!q.empty()) {
		int cur = q.front();
		q.pop();
		for (int i = 0; G[cur][i]; i++) {
			if (dist[G[cur][i]] == -1) dist[G[cur][i]] = dist[cur] + 1;
			else dist[G[cur][i]] = min(dist[G[cur][i]], dist[cur] + 1);
			if (!vis[G[cur][i]]) {
				q.push(G[cur][i]);
				vis[G[cur][i]] = true;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		printf("%d %d\n", i, dist[i]);
	}
    return 0;
}

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

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

int n, G[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(G[u][v] == 0) continue;
      if(d[v] != INFTY) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for(int i = 0; i < n; i++){
    cont << 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++) G[i][j] = 0;
  }

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

  bfs(0);
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<int> d;
vector<int> f;
vector<bool> used;
vector< vector<int> > v;
int n;
int cnt = 0;

class myqueue{
  public:
  int size;
  int begin;
  int end;
  int v[111111];
  myqueue(){
    size = 111111;
    begin = 0;
    end = 0;      //[begin, end)
  }

  void push(int a){
    v[end] = a;
    end = (end + 1) % size;
  }
  int front(){
    return v[begin];
  }
  void pop(){
    begin = (begin + 1) % size;
  }
  bool isEmpty(){
    return begin == end;
  }
};

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

  cin >> n;
  d = f = vector<int>(n, -1);
  used = vector<bool>(n, false);
  v = vector< vector<int> >(n, vector<int>(n, 0));
  for(int i = 0; i < n; i++){
    int u, k; cin >> u >> k;
    u--;
    for(int j = 0; j < k; j++){
      int a; cin >> a;
      a--;
      v[u][a] = 1;
    }
  }

  myqueue que;
  que.push(0);
  d[0] = cnt;
  used[0] = true;
  while(!que.isEmpty()){
    cnt++;
    int q = que.front(); que.pop();
    for(int i = 0; i < n; i++){
      if(v[q][i] && !used[i]){
        used[i] = true;
        que.push(i);
        d[i] = d[q]+1;
      }
    }
  }

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

  return 0;
}

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

struct node {
	std::vector<int> child;
	int distance;
};

int main() {
	int node_num;

	std::cin >> node_num;

	std::vector<node> n(node_num);

	for (int i = 0; i < node_num; i++) {
		int num, to, node_val;
		std::cin >> node_val >> num;
		for (int j = 0; j < num; j++) {
			std::cin >> to;
			n[node_val - 1].child.push_back(to);
		}
		n[i].distance = -1;
	}

	//???????????????????????¢0
	n[0].distance = 0;
	std::queue<int> q;
	int dist = 1;
	q.push(1);
	while(q.size() != 0){
		int num = q.front();
		q.pop();
		for (std::vector<int>::iterator it = n[num - 1].child.begin(); it != n[num - 1].child.end(); it++) {
			q.push(*it);
			if (n[*it - 1].distance > dist || n[*it - 1].distance == -1) {
				n[*it - 1].distance = dist;
			}
		}
		dist++;
	}

	for (int i = 0; i < node_num; i++) {
		std::cout << i + 1 << " " << n[i].distance << std::endl;
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
static const int N = 100;
static const int INFTY = (1 << 21);
using namespace std;

int n, M[N][N],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++: //
// Created by ?????°?????? on 2016/05/15.
//

#include <iostream>
#include <queue>
using namespace std;
const int N = 100;
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,v,k;
    for (int i = 0; i < n ; ++i) {
        for (int j = 0; j < n ; ++j) {
            M[i][j] = 0;
        }
    }
    cin >> n;
    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 inf 1<<30

int n,M[105][105],d[105];
queue<int> Q;

void dfs(int s){
	d[s]=0;
	Q.push(s);
	int v;
	while(!Q.empty()){
		s=Q.front();Q.pop();
		for(int v=0;v<n;v++){
			if(M[s][v]==0){
				continue;
			}
			if(d[v]!=-1){
				continue;
			}
			d[v]=d[s]+1;
			Q.push(v);
		}
	}
}

int main() {
  cin>>n;
  for(int i=0;i<105;i++){
  	d[i]=-1;
    for(int j=0;j<105;j++){
      M[i][j];
    }
  }
  int u,k,v;
  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;
    }
  }
  dfs(0);
  for(int i=0;i<n;i++){
  	printf("%d %d\n",i+1,d[i]);
  }
}