Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 0
}

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

using namespace std;

const int N = 100;
int n;
int M[N][N], color[N], d[N];

int main(){
  queue<int> q;
  cin >> n;
  int u, k, v;
  for(int i=0;i<n;i++){
    cin >> u >> k;
    for(int j=0;j<k;j++){
      cin >> v;
      M[u-1][v-1] = 1;
    }
  }
  for(int i=0;i<n;i++) d[i] = -1;
  d[0] = 0;
  color[0] = 1;
  q.push(0);
  int x;
  while(q.empty() == false){
    x = q.front();
    q.pop();
    for(int i=0;i<n;i++){
      if(M[x][i] == 0) continue;
      if(color[i] == 0){
        color[i] = 1;
        d[i] = d[x] + 1;
        q.push(i);
      }
    }
  }
  for(int i=0;i<n;i++){
    cout << i+1 << " " << d[i] << endl;
  }
  return 0;
}

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

using namespace std;

int n;
int vertex[101];
int graph[101][101];
int dis[101];

queue<int> Q;

int main() {
	cin >> n;
	int k,d;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			graph[i][j] = -1;
		}
		dis[i] = -1;
	}


	for (int i = 1; i <= n; ++i) {
		cin >> vertex[i] >> k;
		for (int j = 0; j < k; ++j) {
			cin >> d;
			graph[i][d] = 1;
		}
	}

	Q.push(1);
	dis[1] = 0;

	while (!Q.empty()) {
		k = Q.front();
		Q.pop();
		for (int i = 1; i <= n; ++i) {
			if (graph[k][i] == 1 && dis[i] == -1) {
				Q.push(i);
				dis[i] = dis[k]+1;
			}
		}
	}
	for (int i = 1; i <= n; ++i) {
		cout << i << ' ' << dis[i] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a), i##_len=(n); i>i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 101;
vi M(MAX, -1);
int main() {
	int n; cin >> n;
	M[1] = 0;
	deque<pair<int, int>> dq;
	int u, k, v;
	rep(i, 0, n) {
		cin >> u >> k;
		rep(j, 0, k) {
			cin >> v;
			if(u == 1) M[v] = 1;
			else dq.eb(mp(u, v));
		}
	}
	while(!dq.empty()) {
		tie(u, v) = dq.front(); dq.pop_front();
		if(M[u] == -1) dq.eb(mp(u, v));
		else if(M[v] == -1 || M[v] > M[u] + 1) M[v] = M[u] + 1;
	}
	rep(i, 1, n + 1) {
		cout << i << " " << M[i] << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<queue>
#define MAX 100
#define INFTY -1

using namespace std;
queue<int> Q;
int d[MAX];
int A[MAX][MAX]={0};
int n;

void bfs(int u)
{
	Q.push(u);
	d[u] = 0;

	while(!Q.empty())
	{
		int v = Q.front();
		Q.pop();
		for(int i=0; i<n; i++)
		{
			if(A[v][i] == 1)
			{
				if(d[i] == INFTY)
				{
					Q.push(i);
					d[i] = d[v] + 1;
					//printf("%d %d\n",v+1,d[v]);
				}
			}
		}
	}
}

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

	for(int i=0; i<n; i++)
	{
		printf("%d %d\n", i+1, d[i]);
	}

	return 0;
}

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

int n,u,k,v;
int g[102][102];
int d[102];
int sn[102];
int cnt=0;
int x,siz;

queue<int>q;

int main(){
  cin>>n;
  memset(g,0,sizeof(g));
  for(int i=1;i<=n;i++){
    cin>>u>>k;
    d[i]=-1;
    sn[i]=0;
    for(int j=1;j<=k;j++){
      cin>>v;
      g[u][v]=1;
    }
  }
  
  q.push(1);

  while(!q.empty()){

    siz=q.size();
    cout<<siz<<endl;

    for(int i=0;i<siz;i++){
      
      x=q.front();
      q.pop();
      
      sn[x]=1;
      if(d[x]==-1)d[x]=cnt;
      
      for(int j=1;j<=n;j++){
	
	if(g[x][j]==1 && sn[j]==0){
	  q.push(j);
	}
      
      }

    }

    cnt++;

  }

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

  }

}

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

using namespace std;

#define MAX 100

#define WHITE 0
#define GRAY 1
#define BLACK 2

int T;
int N;

int M[MAX][MAX];
int C[MAX],D[MAX];

void bfs(int s)
{
    queue<int> *q = new queue<int>;

    q->push(s);
    D[s] = 0;
    C[s] = GRAY;

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

        for (int v = 0; v < N; v++)
        {
            if (M[u][v] && C[v] == WHITE)
            {
                C[v] = GRAY;
                D[v] = D[u] + 1;
                q->push(v);
            }
        }

        C[u] = BLACK;
    }
}

int main ()
{
    cin >> N;

    for (int i = 0; i < N; i++)
    {
        C[i] = WHITE;
        D[i] = -1;

        for (int j = 0; j < N; j++)
        {
            M[i][j] = 0;
        }
    }

    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--;
            M[u][v] = 1;
        }
    }

/*
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (j > 0)
                cout << " ";
            cout << M[i][j];
        }
        cout << endl;
    }
*/
    bfs(0);

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

    return 0;
}

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


int main() {
	int k[110][110];
int n,l,a,b;
cin>>n;
for(int j=0;j<n;j++){
	cin>>l>>a;
	for(int i=0;i<a;i++){
		cin>>b;
		k[j][b-1]=1;
	}
}
int q[105];
for(int j=0;j<n;j++){
	q[j]=-1;
	if(k[0][j]==1){
		q[j]=1;
	}
}
q[0]=0;
int u=1;
int m=1;
while(m<=n){

	for(int d=0;d<n;d++){
		if(q[d]==m){
			for(int h=0;h<n;h++){
				if(k[d][h]==1 &&q[h]==-1){
					q[h]=m+1;
				}
			}
		}
	}
m=m+1;	
}
for(int y=0;y<n;y++){
	cout<<y+1<<" "<<q[y]<<endl;
}

	return 0;
}