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<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()){
    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<k;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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

vector<int>to[105];
int main(){
  int n;
  cin>>n;
  rep(i,n){
    int u,k;
    cin>>u>>k;
    --u;
    rep(j,k){
      int v;
      cin>>v;
      --v;
      to[u].push_back(v);
      //to[v].push_back(u);
    }
  }
  queue<int>q;
  vector<int>dist(n,-1);
  dist[0]=0;
  q.push(0);
  while(!q.empty()){
    int v=q.front();q.pop();
    for(int u:to[v]){
      if(dist[u]!=-1)continue;
      dist[u]=dist[v]+1;
      q.push(u);
    }
  }
  rep(i,n)cout<<i+1<<" "<<dist[i]<<endl;
  return 0;
}

C++: #include<iostream>
#include<queue>
#include<algorithm>
#define INF 999999999
using namespace std;

int res[101];
int G[101][101] = {0};

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

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

    Q.push(0);
    res[0] = 0;

    while (! Q.empty()) {
        u = Q.front();
        for (int i = 0; i < n; i++) {
            if (G[u][i]) {
                Q.push(i);
                res[i] = std::min(res[i], res[u] + 1);
                G[u][i] = 0;
            }
        }
        Q.pop();
    }

    for (int i = 0; i < n; i++) {
        if (res[i] != INF) std::cout << i + 1 << " " << res[i] << "\n";
        else std::cout << i + 1 << " -1\n";
    }

    return 0;
}

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

const int MAX = 105;
int is_visited[MAX];
int dist[MAX];
int am[MAX][MAX];
queue<int> Q;
int t, n;
int ns[MAX];

int next(int u) {
  for (int i = ns[u]; i < n; i++) {
    ns[u] = i + 1;
    if (am[u][i] == 1) {
      return i;
    }
  }
  return -1;
}

void bfs_visit(int r) {
  int current = r;

  dist[current] = 0;
  is_visited[r] = 1;
  for (int i = 0; i < n; i++) {
    if (am[current][i] == 1) {
      Q.push(i);
      dist[i] = dist[current] + 1;
      is_visited[i] = 1;
    }
  }

  while (!Q.empty()) {
    current = Q.front();
    Q.pop();
    for (int i = 0; i < n; i++) {
      if (am[current][i] == 1 && is_visited[i] == 0) {
        Q.push(i);
        dist[i] = dist[current] + 1;
        is_visited[i] = 1;
      }
    }
  }
}

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

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

  bfs_visit(0);

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

C++: #define N 1000
#define M -1
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int m[N][N] = {};
int d[N] = {};
int n;

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++)d[i] = M;
	int j;
	for (int i = 0; i < n; i++) {
		int k;
		int u;
		int v;
		cin >> u;
		cin >> k;
		for (j = 0; j < k; j++) {
			cin >> v;
			m[u][v] = 1;
		}
	}
	queue<int> q;
	q.push(1);
	d[1] = 0;
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		if (d[u] != M) {
			for (int i = 1; i <= n; i++) {
				if (m[u][i]) {
					if (d[i] == M) {
						d[i] = d[u] + 1;
						q.push(i);
						//cout << "a" << endl;
					}
				}
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << i << " " << d[i] << endl;
	}
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include<stdio.h>
#include<string>
#include<iostream>
#include<cctype>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include <algorithm>
#include<math.h>
#include<set>
#include<map>
#include<iomanip>

//#include<bits/stdc++.h>



using namespace std;




int main() {
	
	int n;
	cin>>n;
	vector<vector<int>>t(n,vector<int>(0));
	for(int i=0;i<n;i++){
		int a,b;
		cin>>a>>b;
		for(int j=0;j<b;j++){
			int x;
			cin>>x;x--;t[i].push_back(x);
		}
	}
	queue<int>r;
	r.push(0);
	int g[105]={1000000};
	for(int i=0;i<n;i++){
		g[i]=10000000;
	}
	g[0]=0;
	set<int>p;
	p.insert(0);
	while(r.size()||p.size()<n){
		if(!r.size()){
			for(int i=0;i<n;i++){
				if(g[i]==10000000){
					g[i]=-1;
					p.insert(i);
					//r.push(i);
				}
			}
		}
		else{
		int v=r.front();
		for(int i=0;i<t[v].size();i++){
			r.push(t[v][i]);
			p.insert(t[v][i]);
			g[t[v][i]]=min(g[t[v][i]],g[v]+1);
			
		}
		r.pop();
		
		}
	}
	for(int i=0;i<n;i++)
		cout<<i+1<<" "<<g[i]<<endl;

	
	return 0;
}