Go: package main

import "fmt"

type container struct {
	data []int
}

func (c *container) queue(v int) {
	c.data = append(c.data, v)
}
func (c *container) dequeue(p *int) bool {
	if p == nil || len(c.data) == 0 {
		return false
	}
	*p = c.data[0]
	c.data = c.data[1:len(c.data)]
	return true
}

func main() {
	var n int
	fmt.Scan(&n)
	g := make([][]int, n+1) // 1-indexed
	d := make([]int, n+1)   // 1-indexed
	for i := 1; i <= n; i++ {
		d[i] = -1
		var u, k, v int
		fmt.Scan(&u, &k)
		for j := 0; j < k; j++ {
			fmt.Scan(&v)
			g[u] = append(g[u], v)
		}
	}

	var q container
	d[1] = 0
	q.queue(1)

for len(q.data) > 0  { // 幅優先探索
		var v int
		q.dequeue(&v)
		for i := 0; i < len(g[v]); i++ {
			nv := g[v][i]
		if d[nv] == -1 { // 未訪問
				q.queue(nv)
				d[nv] = d[v] + 1
			}
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

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

int time, n;
int d[100], color[100];
int M[100][100];

void bfs(int s) {
    std::queue<int> Q;
    
    for (int i=0; i<100; i++){
        color[i] = 0;
        d[i] = -1;
    }
    
    color[s] = 1;
    d[s] = 0;
    Q.push(s);
    while (!Q.empty()) {
        int u = Q.front(); Q.pop();
        for (int i=0; i<n; i++) {
            if(M[u][i] && color[i] == 0) {
                color[i] = 1;
                d[i] = d[u]+1;
                Q.push(i);
            }
        }
        color[u] = 2;
    }
}

int main() {
    int u, k, v;
    scanf("%d", &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++) {
        scanf("%d %d", &u, &k);
        for (int j=0; j<k; j++) {
            scanf("%d", &v);
            M[u-1][v-1] = 1;
        }
    }
    
    bfs(0);
    
    for (int i=0; i<n; i++) {
        printf("%d %d\n", i+1, d[i]);
    }
    return 0;
}

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

using namespace std;

int D[110]; // ?????¢????¨???¶
int v, A[100][100], u, k, w;
queue<int> Q;

void bfs(int src) {

	Q.push(src);
	D[src] = 0;

	while (!Q.empty()) {
		int cur = Q.front();
		Q.pop();
		for (int i = 0; i < v; ++i) {
			if (A[cur][i] == 1 && D[i] == 0) {
				D[i] = D[cur] + 1;
				Q.push(i);
			}
		}
	}

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

}

int main(){
	cin >> v;
	// ???????????§??????v
	for (int i = 0; i < v; ++i) {

		cin >> u >> k;
		// ??????????????????u???k?????\?????????????????°??????

		for (int j = 0; j < k; ++j) {

			cin >> w;
			A[u - 1][w - 1] = 1;

			// ??£??\???????????§?¨??????????????????????????????????

		}
	}

	
	bfs(0);

}

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

int k[1000][1000], n, a, b, c, t, x[1000];
queue<int>Q;
queue<int>R;

int main() {
	memset(x, -1, sizeof(x));
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a >> b;
		for (int j = 0; j < b; j++) {
			cin >> c;
			k[i][c] = 1;
		}
	}
	Q.push(1);
	x[1] = 0;
	while (!Q.empty()) {
		t++;
		while (!Q.empty()) {
			for (int i = 1; i <= n; i++) {
				if (x[i] == -1 && k[Q.front()][i] == 1) {
					x[i] = t;
					R.push(i);
				}
			}
			Q.pop();
		}
		while (!R.empty()) {
			Q.push(R.front());
			R.pop();
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << i << ' ' << x[i] << endl;
	}
	return 0;
}

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;
int M[100][100] = {};
int color[100] = {};
int d[100] = {};
int n;
static const int INTFY = -1;


void bfs(int s) {
	int u;
	queue<int> q;
	color[s] = 1;
	d[s] = 0;
	q.push(s);
	while (!q.empty()) {
		u = q.front();q.pop();
		for (int v = 0;v < n;v++) {
			if (M[u][v] == 1 && color[v] == 0) {
				color[v]++;
				d[v] = d[u] + 1;
				q.push(v);
			}
			color[u]++;
		}
	}
	for (int i = 0;i < n;i++) {
		cout << i + 1 << " " << d[i] << endl;
	}
}
int main() {
	int k, m, j;
	cin >> n;
	for (int i = 0;i < n;i++) {
		cin >> m >> k;
		for (int l = 0;l < k;l++) {
			cin >> j;
			M[m - 1][j - 1] = 1;
		}
	}
	for (int i = 0;i < n;i++) {
		d[i] = INTFY;
	}
	bfs(0);

	


	return 0;
}

C++: #include<bits/stdc++.h>
#define ll long long int
#define FOR(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n)

using namespace std;

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


int n, M[N][N], d[N];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  REP(i, n) d[i] = INFTY;
  d[s] = 0;
  int u;
  while ( !q.empty() ) {
    u = q.front();
    q.pop();
    REP(v, n) {
      if ( M[u][v] == 0 ) continue;
      if ( d[v] != INFTY ) continue;
      d[v] = d[u] + 1;
      q.push( v );
    }
  }
  REP(i, n) {
    cout << i+1 << " " << ( (d[i] == INFTY) ? (-1) : d[i] ) << endl;
  }
}

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

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

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

  bfs(0);
  return 0;
}

C++: # include <iostream>
# include <vector>
# include <cstdio>
# include <queue>
using namespace std;
struct info
{
	int d = -1;
	bool vis = false;
};
vector<int> a[105];
info *ans;
queue<int> solve;
void bfs()
{
	solve.push(1); 
	ans[1].vis = true; ans[1].d = 0;
	while(!solve.empty())
	{
		int u = solve.front(); solve.pop();
		for(int j = 0;j < a[u].size();j++)
		{
			if(!ans[a[u][j]].vis)
			{
				ans[a[u][j]].vis = true;
				ans[a[u][j]].d = ans[u].d + 1;
				solve.push(a[u][j]);
			}
		}
	}
	
}
int main(void)
{
	int n,cnt,k,u;
	scanf("%d",&n);
	ans = new info [n+1];
	for(int w = 0;w < n;w++)
	{
		scanf("%d %d",&u,&cnt);
		for(int i = 0;i < cnt;i++)
		{
			scanf("%d",&k);
			a[u].push_back(k);
		}
	}
	bfs();
	for(int i = 1;i <= n;i++) printf("%d %d\n",i,ans[i].d);
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#define MAX 101
using namespace std;

int n;
int a[MAX][MAX]={};
int d[MAX];

void track(int v,int depth){
  if(d[v]>depth||d[v]==-1) d[v]=depth;
  for(int i=1;i<=n;i++){
    if(a[v][i]==1){
      track(i,depth+1);
    }
  }
}

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

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

  track(1,0);

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