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;

vector< vector < int > > adjList;
vector< int > depths;

void bfs(void) {
  queue< int > currentLevel;
  queue< int > nextLevel;
  nextLevel.push(1);
  int depth = 0;
  while (nextLevel.size()) {
    while (nextLevel.size()) {
      currentLevel.push(nextLevel.front());
      nextLevel.pop();
    }
    while (currentLevel.size()) {
      if (depths[currentLevel.front()-1] == -1) {
	depths[currentLevel.front()-1] = depth;
	for (int i = 0; i < adjList[currentLevel.front()-1].size(); i++)
	  nextLevel.push(adjList[currentLevel.front()-1][i]);
      }
      currentLevel.pop();
    }
    depth++;
  }
}

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  adjList = vector< vector < int > > (n, vector< int > ());
  depths = vector< int > (n, -1);
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int holder;
      cin >> holder;
      adjList[i].push_back(holder);
    }
  }
  bfs();
  for (int i = 0; i < n; i++)
    cout << i+1 << ' ' << depths[i] << endl;
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <string.h>

/*
 * ref. http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_B
 */

using namespace std;

const int MAX_N = 100;

int n;
vector<int> adjlist[MAX_N];
int d[MAX_N];
queue<int> que;

void bfs() {
    int id = 1;

    d[id - 1] = 0;
    // for (int i = 0; i < (int) adjlist[id - 1].size(); i++) {
    //     int next_id = adjlist[id - 1][i];
    //     if (d[next_id - 1] < 0 || d[next_id - 1] > d[id - 1] + 1) {
    //         d[next_id - 1] = d[id - 1] + 1;
    //         que.push(next_id);
    //     }
    // }
    que.push(id);

    while (!que.empty()) {
        id = que.front();
        que.pop();

        for (int i = 0; i < (int) adjlist[id - 1].size(); i++) {
            int next_id = adjlist[id - 1][i];
            if (d[next_id - 1] < 0 || d[next_id - 1] > d[id - 1] + 1) {
                d[next_id - 1] = d[id - 1] + 1;
                que.push(next_id);
            }
        }
    }
}

int main(void) {
    cin >> n;

    for (int i = 0; i < n; i++) {
        int id, m;
        cin >> id >> m;
        for (int j = 0; j < m; j++) {
            int x;
            cin >> x;
            adjlist[id - 1].push_back(x);
        }
    }

    memset(d, -1, sizeof(d));
    bfs();

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

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int MOD =1e9+7;
using ll=long long;
using Graph = vector<vector<int>>;

int main() {
    int n; 
    cin >> n;
    Graph G(n);
    
    rep(i,n){
        int N; cin >> N; N--;
        int k; cin >> k;
        rep(j,k){
            int v; cin >> v; v--;
            G[N].push_back(v);
        } 
    }

    vector<int> dist(n, -1);
    queue<int> que;
    dist[0] = 0;
    que.push(0);

    while(!que.empty()){
        int v = que.front(); que.pop();

        for(int nv :G[v]){
            if(dist[nv] != -1) continue;

            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }
    rep(i,n){
        cout << i+1 <<" "<< dist[i] << endl;
    }
}

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

using namespace std;

#define N 100
#define INF -1

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

void bfs(int s){
	for(int i=0;i<n;i++){
		d[i] = INF;
	}
	int u,v;
	queue<int> q;
	q.push(s);
	d[s] = 0;
	while( !q.empty() ){
		u = q.front();
		q.pop();
		for(v=0;v<n;v++){
			if(d[v] != INF || m[u][v] != 1)continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}
	for(int i=0;i<n;i++){
		printf("%d %d\n",i+1,d[i]);
	}
}

int main(void){
	int u,k,v;
	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			m[i][j] = 0;
		}
	}
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d %d",&u,&k);
		u--;
		for(int j=0;j<k;j++){
			scanf("%d",&v);
			v--;
			m[u][v] = 1;	
		}
	}
	bfs(0);
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <cstdlib>
#include <queue>
#include <vector>
#include <set>

using namespace std;
int n,m,l,p;
bool a[100][100];
int D[100];
void  bfs ( int src) {
	queue<int> Q;
	Q.push(src);
	D[src]=0;
	while( ! Q.empty() ) {
		int cur = Q.front();
		Q.pop();
		for(int dst = 0; dst < n; dst++ ) {
			if( D[dst] == 0 && a[cur][dst] == 1 ) {
				D[dst] = D[cur] + 1;
				Q.push(dst);
			}
		}
	}
	for (int i = 0; i < n;i++) {
	cerr << i+1 <<  " " << D[i] <<  endl;
	}
}
int main() {
	cin >> n;
	for ( int i = 0;i < n;i++) {
		for(int j= 0; j < n;j++) {
			a[i][j] = 0;
		}
	}
	for (int i = 0;i < n; i++){
		cin >> m;
		cin >> l;
		for(int j = 0; j < l;j++) {
			cin >> p;
			a[m-1][p-1] = 1;
		}
	}
	bfs(0);
}

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

using namespace std;

int d[100];
vector<int> adj[100];

void bfs(int n, int start) {
	queue<int> que;

	for (int i = 0; i < n; i++)
		d[i] = i == start ? 0 : -1;

	que.push(start);
	while(!que.empty()) {
		int from = que.front();
		que.pop();

		for (int& to : adj[from])
			if (d[to] == -1) {
				d[to] = d[from] + 1;
				que.push(to);
			}
	}
}

int main() {
	int n;

	cin >> n;

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		while (k--) {
			int v;
			cin >> v;
			adj[u - 1].push_back(v - 1);
		}
	}

	bfs(n, 0);

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

	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

const int INF = 1 << 30;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 100;

vector<int> G[N];
int d[N];

int main() {
	int n; cin >> n;
	rep(i, n) {
		int _, n2; cin >> _ >> n2;
		rep(j, n2) {
			int k; cin >> k; k--;
			G[i].push_back(k);
		}
	}
	rep(i, n) d[i] = -1;

	queue<int> Q;
	Q.push(0);
	d[0] = 0;
	while (!Q.empty()) {
		int i = Q.front(); Q.pop();
		for (auto j : G[i]) {
			if (d[j] != -1) continue;
			d[j] = d[i] + 1;
			Q.push(j);
		}
	}
	rep(i, n) cout << i+1 << ' ' << d[i] << endl;
}