Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

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;
	while(r.size()){
		int v=r.front();
		for(int i=0;i<t[v].size();i++){
			r.push(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;
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);

int M[N][N] = {0}, d[N], n;

void bfs() {
  int u, v;
  queue<int> q;
  for (u = 0; u < n; u++) {
    d[u] = INFTY;
  }
  d[0] = 0;
  q.push(0);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (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 (u = 0; u < n; u++) {
    cout << u + 1 << ' ' << ((d[u] == INFTY) ? (-1) : d[u]) << endl;
  }
}

int main() {
  int u, v, k, i, j;

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

  bfs();

  return 0;
}

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

typedef struct{
  int A[1000];
  int ans;
}node;
node T[1000];
int Q[1000000];
int head=0;
int tail=1;
int n;

void function(int num){
  while(true){
    if(head==tail)break;
    for(int i=1;i<=n;i++){
      if(T[num].A[i]==1){
	if(T[i].ans==0){
	  T[i].ans=T[num].ans+1;
	}
	Q[tail++]=i;
      }
    }
    num=Q[head++];
  }
  
}

int main(){
  int num;
  int son;
  int z;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>num;
    cin>>son;
    for(int j=0;j<son;j++){
      cin>>z;
      T[num].A[z]=1;
      }
  }

  function(1);
  for(int i=2;i<=n;i++){
    if(T[i].ans==0){
      T[i].ans=-1;
    }
  }

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


  return 0;
}

C++: //16D8103013E Sen Shunkou
#include<iostream>
#include<queue>
int n;
int cnt=0;

int main(){
    //input
    int n;
    std::cin>>n;

    int cnt=0;
    int A[100][100];
    int dis_min[n];
    bool visited[n];

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            A[i][j]=0;
        }
    }

    for(int i=0;i<n;i++){
        std::cin>>A[i][0];
        std::cin>>A[i][1];
        for(int j=0;j<A[i][1];j++){
            std::cin>>A[i][j+2];
        }
    }

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

    //initialization
    std::queue<int> que;
    que.push(0);
    //std::cout<<"\n"<<que.front()<<"\n";
    //std::cout<<que.size()<<"\n\n";

    while(!que.empty()){
        int q=que.size();
        for(int k=0;k<q;k++){
            //std::cout<<"quesize"<<que.size()<<"\n";
            int i=que.front();
            que.pop();
            visited[i]=true;
            dis_min[i]=cnt;
            //std::cout<<i<<"cnt"<<cnt<<"\n";
            if(A[i][1]!=0){
                for(int j=0;j<A[i][1];j++){
                    if(visited[A[i][j+2]-1]!=true){
                        visited[A[i][j+2]-1]=true;
                        que.push(A[i][j+2]-1);
                        //std::cout<<A[i][j+2]-1<<"\n";
                    }
                }
            }
        }
        cnt++;
        //std::cout<<"cnt="<<cnt<<"\n";
    }

    //std::cout<<"===========\n";
    for(int i=0;i<n;i++){
        std::cout<<i+1<<" "<<dis_min[i]<<std::endl;
    }
    return 0;
}

C++: /*
　　こんにちは。あたしはｶｳｶﾞｰﾙ。
　　redcoderになるためAtCoderを巡る旅をしてます。

　　　 　 ＿_
　　　 ヽ|＿_|ノ　　　　ﾓｫ
　　　　||‘‐‘||ﾚ　　_)_, ―‐ 、
　　　　/(Ｙ (ヽ＿ /・ ヽ　　 ￣ヽ
　　　　∠ ＿ ゝ　 ｀^ヽ ﾉ.::::_(ノヽ
　　　　 _/ヽ　 　　  /ヽ￣￣/ヽ
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define dump(x) cout << #x << " = " << (x) << endl
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl

#define rep(i, n) REP(i, 0, n)                  // 0, 1, ..., n-1
#define REP(i, x, n) for(int i = x; i < n; i++) // x, x + 1, ..., n-1
#define FOREACH(x, a) for(auto &(x) : (a))

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()

#define COUT(x) cout << (x) << endl

template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}

using Graph = vector<vector<int>>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    Graph G(n);
    rep(i, n) {
        int u, k;
        cin >> u >> k;
        u--;
        rep(j, k) {
            int v;
            cin >> v;
            v--;
            G[u].push_back(v);
        }
    }

    vector<int> dist(n, -1);
    queue<int> que;

    dist[0] = 0;
    que.push(0);

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

        for(auto nv : G[v]) {
            if(dist[nv] != -1) continue;
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }

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

C++: #include <iostream>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
using namespace std;

const int MAX = 110;
int mat[MAX][MAX];
int dist[MAX], done[MAX];
int n;

void bfs(int s)
{
  int t = 0;
  queue<int> que;
  que.push(s);
  dist[s] = 0;
  while (!que.empty()) {
    int i = que.front(); que.pop();
    if (done[i]) continue;
    done[i] = 1;
    for (int j = 0; j < n; j++) {
      if (mat[i][j]) {
        dist[j] = min(dist[j], dist[i]+1);
        que.push(j);
      }
    }
  }
}

int main()
{
  cin >> n;

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

  fill(dist, dist+MAX, INT_MAX);
  memset(done, 0, sizeof(done));
  bfs(0);
  for (int i = 0; i < n; i++)
    printf("%d %d\n", i+1, (dist[i]==INT_MAX?-1:dist[i]));

  return EXIT_SUCCESS;
}