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++: #include<iostream>
using namespace std;

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

void function(int num){
  while(true){
    if(head==tail)break;
    num=Q[head++];
    T[num].c=true;
    for(int i=2;i<=n;i++){
      if(T[num].A[i]==1&&T[i].c==false){
	if(T[i].ans==0){
	  T[i].ans=T[num].ans+1;
	  T[num].A[i]=0;
	}
	Q[tail++]=i;
      }
    }
    //    cout<<"Q "<<Q[head]<<endl;
    //  cout<<head<<" "<<tail<<endl;
  }

}

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;
      }
  }
  Q[0]=1;
  function(1);
  T[1].ans=0;
  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++: #include <bits/stdc++.h>
using namespace std;

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

  vector<vector<int>> M(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    int node;
    int d;
    cin >> node >> d;
    node--;

    for (int j = 0; j < d; j++) {
      int tmp;
      cin >> tmp;
      tmp--;
      M[i][tmp] = 1;
    }
  }

  vector<int> res(n, -1);
  vector<int> e(n);
  res[0] = 0;

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

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

    int cnt = res[num] + 1;

    for (int i = 0; i < M[num].size(); i++) {
      if (M[num][i] == 0) { continue; }
      if (e[i] == 1) { continue; }
      q.push(i);
      e[i]   = 1;
      res[i] = cnt;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << res[i] << 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 <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define invrepr(i,a,b) for(int i=b-1;i>=a;i--)
#define invrep(i,n) invrepr(i,0,n)
#define repitr(itr,a) for(auto itr=a.begin();itr!=a.end();++itr)
const ll MOD=1e9+7;

int main() {
    ios_base::sync_with_stdio(false);

    int n;
    cin >> n;
    vector<vector<int>> r(n);
    rep(i,n) {
        int u,k,v;
        cin >> u >> k;
        rep(j,k) {
            cin >> v;
            r[i].push_back(v-1);
        }
    }
    queue<int> q;
    vector<int> cost(n,-1);
    q.push(0);
    cost[0]=0;
    while (!q.empty()) {
        int now=q.front();
        q.pop();
        rep(i,r[now].size()) {
            int next=r[now][i];
            if (cost[next]==-1) {
                cost[next]=cost[now]+1;
                q.push(next);
            }
        }
    }
    rep(i,n) cout << i+1 << ' ' << cost[i] << endl;
    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
using namespace std;

#define REP(i,n) for(int i=0;(i)<(n);(i)++)
#define ll long long

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

int n, M[100][100];

void BFS(void) {
	queue<int> Q;
	int d[100];
	REP(i, n) d[i] = INFTY;
	d[0] = 0;

	Q.push(0);//?§???????0

	int u;
	while (!Q.empty()) {
		u = Q.front(); Q.pop();
		REP(i, n) {
			if (M[u][i]) {
				if (d[i] == INFTY) {
					d[i] = d[u] + 1;
					Q.push(i);
				}
			}
		}
	}

	REP(i, n) {
		cout << i + 1 << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
	}
}

int main() {
	cin >> n;

	REP(i, n) {
		REP(j, n) M[i][j] = 0;
	}

	int u, k, v;
	REP(i, n) {
		cin >> u >> k;
		REP(j, k) {
			cin >> v;
			M[u - 1][v - 1] = 1;//0-origin
		}
	}

	BFS();

	return 0;
}

C++: #include <iostream>
#include <queue> 
using namespace std;
int V[100][100];
int d[100];
int t[100];
int n;
queue<int> qu;
void bfs()
{
	t[0] = 1;
	d[0] = 0;
	qu.push(0);
	while (!qu.empty())
	{
		int u = qu.front(); qu.pop();
		for (int i = 0; i < n; i++)
		{
			if (V[u][i] == 1 && t[i] == 0)
			{
				t[i] = 1;
				d[i] = d[u] + 1;
				qu.push(i);
			}
		}
		t[u] = 2;
	}
	return;
}

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

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

	for (int i = 0; i < n; i++)
	{
		d[i] = -1;
		t[i] = 0;
	}
	bfs();
	for (int i = 0; i < n; i++)
	{
		cout << i + 1 << " " << d[i] << endl;
	}
	return 0;
}