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 <stdio.h>
#include <queue>
 
using namespace std;
#define N 100
#define INF 2000000
 
int n, M[N][N];
int d[N];
 
void bfs(int s){
    int i,u,v;
 
    queue<int> q;
    q.push(s);
    for(i = 0; i < n; i++){
        d[i] = INF;
    }
 
    d[s] = 0;
 
    while(!q.empty()){
        u = q.front();
        q.pop();
        for(v = 0; v < n; v++){
            if(M[u][v] == 0) continue;
            if(d[v] != INF) continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for(i = 0; i < n; i++){
        if(d[i] == INF){
            printf("%d -1\n", i+1);
        }else{
            printf("%d %d\n", i+1, d[i]);
        }
    }
}
 
 
int main(void){
    int i,j,u,k,v;
 
    scanf("%d", &n);
 
    for(i = 0;i < n; i++){
        for(j = 0; j < n; j++){
            M[i][j] = 0;
        }
    }
 
    for(i = 0; i < n; i++){
      cin >> u >> k;
        u--;
        for(j=0;j<k;j++){
          cin >> v;
            v--;
            M[u][v] = 1;
        }
    }
 
    bfs(0);
 
    return 0;
}

C++: #include<iostream>
#include<stdio.h>

using namespace std;
int d[101];

int n;
int M[101][101];
int Q[101];
int head = 0;
int tail = 0;

void enqueue(int x) {
	Q[tail] = x;
	if (tail == (tail - head)) {
		tail = 1;
	}
	else {
		tail = tail + 1;
	}
}

void BFS(int s) {
	enqueue(s);
	d[s] = head;
	while (head != tail) {
		s = Q[head];
		head = head + 1;
		
		for (int j = 1; j < n + 1; j++) {
			if (M[s][j] == 1) {
				if(d[j] == -1){
					d[j] = d[s] + 1;
					enqueue(j);
				}

			}
		}

	}
}

int main() {
	int  u, k, v;//頂点数、頂点の番号、出次数、隣接する頂点の番号
	

	cin >> n;



	//初期化
	for (int i = 0; i < 101; i++) {
		d[i] = -1;
		
		for (int j = 0; j < 101; j++) {
			M[i][j] = 0;

		}
	}


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


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

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<iomanip>
#include<set>
#include<numeric>
#include<sstream>
#include<random>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
const int inf = 1e9 + 7;
int dy[] = {0, 0, -1, 1};
int dx[] = {1, -1, 0, 0};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    int table[n + 1][n + 1]; rep(i, n + 1) rep(j, n + 1) table[i][j] = 0;
    rep(j, n) {
        int u, k; cin >> u >> k;
        int v;
        rep(l, k) {
            cin >> v;
            table[u][v] = 1;
        }
    }
    int mc[n + 1]; rep(i, n + 1) mc[i] = inf;
    queue<int> q;
    q.push(1);
    mc[1] = 0;
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        rrep(i, 2, n + 1) {
            if (table[now][i] == 1 && mc[i] == inf) {
                mc[i] = mc[now] + 1;
                q.push(i);
            }
        }
    }
    rrep(i, 1, n + 1) {
        cout << i << " " << (mc[i] == inf ? -1 : mc[i]) << endl;
    }
}

C++: #include <iostream>
#include <queue>
using namespace std;
int D[110]={0};
void bfs(int src,int n,int g[110][110]){
queue<int> Q;
Q.push(src);
D[src]=0;
while (! Q.empty()){
    int cur =Q.front();
    Q.pop();
    for(int r=0;r<n;r++){
        if (g[cur][r]==1&&D[r]==0){
            D[r]=D[cur]+1;
            Q.push(r);
        }
    }
}
}
int main() {
int n,g[110][110]={0};
cin>>n;
int j,num,t;
for(int k=0;k<n;k++){
cin>>j>>num;
for(int kk=0;kk<num;kk++){
    cin>>t;
    g[k][t-1]=1;
}
}
bfs(0,n,g);

for(int a=0;a<n;a++){
    if (D[n]==0){
        D[n]=-1;
}
    D[0]=0;
    cout<<a+1<<" "<<D[a]<<endl;
}
    return 0;
}

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

int n;

int V[100][100];
int D[100];
int d = 0;

std::queue<int> Q;

void bfs(int r) {
    Q.push(r);
    D[r] = 0;
    while (Q.size()) {
        r = Q.front();
        Q.pop();
        for (int i = 0; i < n; i++) {
            if ((D[i] == -1) && (V[r][i] == 1)) {
                Q.push(i);
                D[i] = D[r] + 1;
            }
        }
    }
}


int main() {
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        int id, e_num;
        std::cin >> id >> e_num;
        for (int j = 0; j < e_num; j++) {
            int t;
            std::cin >> t;
            V[id - 1][t - 1] = 1;
        }
    }

    for (int i = 0; i < n; i++) {
        D[i] = -1;
    }

    bfs(0);
    for (int i = 0; i < n; i++) {
        std::cout << i + 1 << " " << D[i] << std::endl;
    }
}

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>
#include <queue>

using namespace std;

#define MAX 100
#define INF -1

int n;
int G[MAX][MAX];
int dist[MAX];

void bfs(){
  bool used[MAX];
  fill(dist,dist+n,INF);
  fill(used,used+n,false);
  dist[0] = 0;
  used[0] = true;

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

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

    for(int i = 0 ; i < n ; i++){
      if(!used[i] && G[v][i]){
	used[i] = true;
	dist[i] = dist[v] + 1;
	Q.push(i);
      }
    }
  }
}

int main(){
  cin >> n;

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

  bfs();

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

  return 0;
}