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 <bits/stdc++.h>

using namespace std;

typedef pair<int, int> P;
typedef long long int LL;

vector<int> v[101];
vector<int> dist;
queue<int> q;

int bfs() {
    dist[1] = 0;
    q.push(1);

    while (!q.empty()) {
        int c = q.front();
        q.pop();
        for (int i = 0; i < v[c].size(); i++) {
            if (dist[v[c][i]] == -1) {
                dist[v[c][i]] = dist[c] + 1;
                q.push(v[c][i]);
            }
        }
    }
}

int main() {
    int n;
    cin >> n;
    dist.resize(n + 1, -1);

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

    bfs();

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

    return 0;
}

C++: //1933-
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define INT(x) int x; scanf("%d",&x)
#define INPUT(x) scanf("%d",&x)
#define REP1(x,n) for(int x = 0; x < n; x++)
#define REP2(x,s,e) for(int x = s; x <= e; x++)
#define BR printf("\n")
#define INF 2000000000
#define FIND 1
#define END 2

vector<int> V[100];
priority_queue<pair<int,int> > PQ;//distance,id
int d[100];
int StateNode[100] = {};

void BFS(pair<int,int> p){
    if(d[p.second] <= -p.first) {
        PQ.pop();
        if(!PQ.empty()) BFS(PQ.top());
        return;
    }
    d[p.second] = -p.first;
    PQ.pop();
    
    REP1(k,V[p.second].size()){
        if (StateNode[V[p.second][k]] == 0 || (StateNode[V[p.second][k]] == FIND && -p.first+1)) {
            PQ.push(make_pair(p.first-1,V[p.second][k]));
            StateNode[V[p.second][k]] = FIND;
        }
    }
    
    if(!PQ.empty()) BFS(PQ.top());
}

int main(){
    INT(N);
    REP1(i,N){
        int from,n;
        scanf("%d %d",&from,&n);
        REP1(j,n){
            INT(to);
            V[i].push_back(to-1);
        }
        d[i] = INF;
    }
    
    PQ.push(make_pair(0,0));
    BFS(PQ.top());
    
    REP1(i,N){
        printf("%d %d\n",i+1,d[i]);
    }
    
    return 0;
}

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

#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define REP(i,n) FOR(i,0,n)
using namespace std;

int main(){
    int n,u,k,v,src=0;
    cin >> n;
    int D[n],g[n][n];
    REP(i,n){
        D[i] = -1;
        REP(j,n){
            g[i][j] = 0;
        }    
    }
    REP(i,n){ 
        cin >> u >> k;
            REP(j,k){
                cin >> v;
                g[u-1][v-1] = 1;
            }
    }

    queue<int> Q;
    Q.push(src);
    D[src] = 0;
    while (!Q.empty()) {
        int cur = Q.front();
        Q.pop();
            REP(i,n){
                if(g[cur][i] == 1 && D[i] < 0){
                    D[i] = D[cur] + 1;
                    Q.push(i);
                }
            }
    }

    REP(i,n){
        cout << i+1 << " " << D[i] << endl;    
    }
}

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

int n;
int u[101], k[101], d[101];
vector<vector<int>> g;
queue<pair<int, int>> q;

int main() {
	cin >> n;
	g.resize(n+1);
	for(int i=1;i<=n;i++){
		d[i] = -1;
		cin >> u[i] >> k[i];
		if(k[i]){
			for(int j=0;j<k[i];j++){
				int v; cin >> v;
				g[u[i]].push_back(v);
			}
		}
	}
	q.push({1,0});
	while(!q.empty()){
		int a = q.front().first;
		d[a] = q.front().second;
		q.pop();
		for(auto c: g[a]){
			if(d[c]!=-1) continue;
			else{
				d[c] = d[a]+1;
				q.push(make_pair(c, d[c]));
			}
		}
	}
	for(int i=1;i<=n;i++){
		cout << i << ' ' << d[i] << endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
//original idea: stupid wanna check the node with many bfs search 
using namespace std;
#define N 105 // number of nodes
vector<int> neighbors[N]; //start from 1
queue<int> q;
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C bfs
int dist[N];
int vis[N];
int res = 0;
void bfs(int d){
	if(q.empty()) return;
	//until q is empty
	while(!q.empty()){
		int temp = q.front();
		q.pop();
		if(dist[temp]!=-1){
			if(d<dist[temp]) dist[temp] = d;
			else {dist[temp] = dist[temp];}
		}else {
			dist[temp] = d;
		}
		for(int i = 0; i<neighbors[temp].size(); i++){
			d = dist[temp] +1;//next lever
		if(dist[neighbors[temp].at(i)] == -1){//not visited
			q.push(neighbors[temp].at(i));
			dist[neighbors[temp].at(i)] = d;
			}
		}
		
	}
	
}

int main()
{
    int n;
    scanf("%d",&n);
    memset(dist, -1, sizeof(dist));
    memset(vis, -1, sizeof(dist));
    for(int i = 0; i<n; i++){
        int node;
        scanf("%d",&node);
        int num;
        scanf("%d",&num);
        for(int j = 0; j<num; j++){
        	int temp;
        	scanf("%d",&temp);
        	neighbors[node].push_back(temp);
        	neighbors[temp].push_back(node);
		}
    }
	q.push(1);//begin from 1
    bfs(0);
    for(int i = 1; i<=n; i++){
    	printf("%d %d\n",i, dist[i]);
	}
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

vector<int> to[100];
int ans[100];

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    int u, k;
    cin >> u >> k;
    vector<int> v(k);
    rep(j, k) {
      int vj;
      cin >> vj;
      vj--;
      v[j] = vj;
    }
    to[i] = v;
    ans[i] = -1;
  }

  queue<int> q;
  q.push(0);
  ans[0] = 0;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int next : to[now]) {
      if (ans[next] != -1) continue;
      ans[next] = ans[now] + 1;
      q.push(next);
    }
  }

  rep(i, n) printf("%d %d\n", i + 1, ans[i]);

  return 0;
}