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++: /*
 * c.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

const int MAX_N = 100;
const int INF = 1 << 30;

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;

/* global variables */

vi nbrs[MAX_N];
int dists[MAX_N];

/* subroutines */

/* main */

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    u--;

    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      v--;
      nbrs[u].push_back(v);
    }
  }

  for (int i = 0; i < n; i++) dists[i] = INF;
  dists[0] = 0;

  qi q;
  q.push(0);

  while (! q.empty()) {
    int u = q.front(); q.pop();

    int vd = dists[u] + 1;
    vi &nbru = nbrs[u];
    for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
      int &v = *vit;
      if (dists[v] > vd) {
	dists[v] = vd;
	q.push(v);
      }
    }
  }

  for (int i = 0; i < n; i++)
    printf("%d %d\n", i + 1, (dists[i] >= INF) ? -1 : dists[i]);

  return 0;
}

C++: #include<cstdio>
#include<queue>
using namespace std;

static const int MAX_N = 100;
static const int INFTY = (1<<21);

int n, adj[MAX_N][MAX_N];
int d[MAX_N];

void dfs(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(adj[u][v] == 0) continue;
			if(d[v] != INFTY) continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}
	for(int i = 0; i < n; i++){
		printf("%d %d\n", i + 1, ((d[i] == INFTY) ? (-1) : d[i]));
	}
}

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

	dfs(0);
	return 0;
}

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

int main() {
    int n;
    cin >> n;

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

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

    while (!q.empty()) {
        int cur = q.front();
        q.pop();
        for (int ch : edge[cur]) {
            if (dist[ch] > dist[cur] + 1) {
                dist[ch] = dist[cur] + 1;
                q.push(ch);
            }
        }
    }

    for (int i = 0; i < n; i++) {
        cout << i + 1 << ' ' << (dist[i] == n ? -1 : dist[i]) << '\n';
    }
    return 0;
}

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

/////////////////////////////////////////////////////////////////////

template<typename T>
vector<T> BFS(vector<vector<int>> &G, int s) {
    vector<T> ret(G.size(),-1);
    ret[s] = 0;
    queue<int> que;
    que.emplace(s);
    while (!que.empty()) {
        int v = que.front(); que.pop();
        for (auto nv : G[v]) {
            if (ret[nv] != -1) continue;
            ret[nv] = ret[v] + 1;
            que.emplace(nv);
        }
    }

    return ret;
}

////////////////////////////////////////////////////////////////////

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N; cin >> N;
    vector<vector<int>> G(N);
    for (int i = 0; i < N; ++i) {
        int u,k; cin >> u >> k;
        for (int j = 0; j < k; ++j) {
            int v; cin >> v;
            --v;
            G[i].emplace_back(v);
        }
    }

    auto dist = BFS<int>(G,0);

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

/*
  verified on 2020/02/18
  http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C&lang=ja
*/

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

using namespace std;
static const int N=100;
static 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,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++: #include <iostream>
using namespace	std;
int n,g[100][100],f[100],c[100];
void bfs(int now,int far){
  f[now]=far;
  c[now]=1;
  far++;
  for(int i=0;i<n;i++) if(g[now][i]&&c[i]!=1) bfs(i,far);

}
int main(){

  int a,b,tmp;
  cin>>n;
  for(int i=0;i<n;i++){
    f[i]=-1;
    cin>>a>>b;
    for(int j=0;j<b;j++){
      cin>>tmp;
      g[a-1][tmp-1]=1;
    }
  }

  bfs(0,0);

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

  return 0;

}

C++: #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[200][200];
    int i, j;
    int u, k, v;
    int dist[200];
    bool visited[200];
    
    for(i = 0; i < n; i++){
        dist[i] = -1;
        for(j = 0; j < n; j++){
            g[i][j] = 0;
        }
    }
    
    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()){
            int 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;
        q2 = queue<int>();
    }
    
    for(i = 0; i < n; i++){
        cout << i+1 << " " << dist[i] << endl;
    }
}