Go: package main

import (
	"container/list"
	"fmt"
	"bufio"
	"os"
	"strconv"
)

const (
	N   = 100
	INF = 1 << 21
)

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(0)
}

C++: #include <bits/stdc++.h>
using namespace std;
const int N = 101;
const int CRL = -1;
int adj[N][N] = {0};
int d[N]={0};
int kai[N];

int main()
{
    int n;
    queue <int> q;
    q.push(1);
    cin >> n;
    for(int i=1;i<=n;i++){
    	int k;
    	int l;
    	cin >> l >> k;
    	kai[i]=l;
    	for(int j=1;j<k+1;j++){
    		int m;
    		cin >> m;
    		adj[i][j] = m;
    	}
    }
    while(q.size()){
    	int k = q.front();
    	q.pop();
    	for(int i=1;i<kai[i]+1;i++){
    		if(d[adj[k][i]]==0){
    			d[adj[k][i]] = d[k]+1;
    			q.push(adj[k][i]);
    		}
    	}
    }
    for(int i=2;i<N;i++)
    	if(d[i]==0)d[i]=-1;
    for(int i=1;i<=n;i++){
    	cout << i << " " << d[i] << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a), i##_len=(n); i>i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 101;
constexpr int INF = (1<<21);
int n;
vv(int, M, MAX, MAX, 0);
vi d(MAX, INF);
inline void bfs() {
	d[1] = 0;
	deque<int> dq;
	dq.eb(1);
	int u;
	while(!dq.empty()) {
		u = dq.front(); dq.pop_front();
		rep(v, 1, n + 1) {
			if(M[u][v] == 0 || d[v] != INF) continue;
			d[v] = d[u] + 1;
			dq.eb(v);
		}
	}
	rep(i, 1, n + 1) {
		cout << i << " " << ((d[i] == INF) ? (-1) : d[i]) << endl;
	}
}
int main() {
	cin >> n;
	int u, k, v;
	rep(i, 1, n + 1) {
		cin >> u >> k;
		rep(j, 1, k + 1) {
			cin >> v;
			M[u][v] = 1;
		}
	}
	bfs();
	rep(i, 1, n + 1) {
		rep(j, 1, n + 1) {
			cout << M[i][j] << (j == n ? "\n" : " ");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
queue<int> q;
vector<vector<int> > v(101);
int dp[101];
int n, t = 0;
int main() {
	cin >> n;
	for (int i = 0; i < n;i++) {
		int u, k; cin >> u >> k;
		for (int j = 0; j < k;j++) {
			int x; cin >> x;
			v[u].push_back(x);
		}
	}
	memset(dp, -1, sizeof(dp));
	q.push(1); dp[1] = 0;
	while (!q.empty()) {
		int a = q.front(); q.pop();
		for (int i = 0; i < v[a].size();i++) {
			if (dp[v[a][i]] == -1) {
				dp[v[a][i]] = dp[a] + 1;
				q.push(v[a][i]);
			}
		}
	}
	for (int i = 1; i <= n;i++) {
		cout << i << " " << dp[i] << endl;
	}
}

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

void visit(int kyori[], vector<list<int> > adj){
    int k;
    queue<int> que;
    list<int>::iterator t;
    que.push(1);
    while(!que.empty()){
        k = que.front();
        que.pop();
        for(t = adj[k].begin(); t != adj[k].end(); ++t){
            if(kyori[*t] == -1){
                kyori[*t] = kyori[k] + 1;
                que.push(*t);
            }
        }
    }

}

int main(){
    //input
    int n;
    cin >> n;
    int u, k, v;
    int i, j;
    list<int> list_sub;
    vector<list<int> > adj;
    for(i = 0; i <= n; i++){
        adj.push_back(list_sub);
    }
    for(i = 1; i <= n; i++){
        cin >> u >> k;
        for(j = 1; j <= k; j++){
            cin >> v;
            adj[i].push_back(v);
        }
    }

    //calc
    int kyori[n+1];
    for(i = 2; i <= n; i++){
        kyori[i] = -1;
    }
    kyori[1] = 0;
    visit(kyori, adj);

    //answer
    for(i = 1; i <= n; i++){
        cout << i << " " << kyori[i] << endl;
    }
    return 0;
}

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

using namespace std;

int main(int argc, char* argv[]) {
  int n;
  cin >> n;

  vector<vector<int> > V(n);
  for (int i = 0; i < n; ++i) {
    int u, k;
    cin >> u >> k;
    V[u - 1].resize(k);
    for (int j = 0; j < k; ++j)
      cin >> V[u - 1][j];
  }

  int d[n];
  memset(d, -1, sizeof(int) * n);
  d[0] = 0;

  queue<int> q;
  q.push(0);

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

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

  return 0;
}