Go: package main

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

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)
	paths[1] = 1

	for len(queue) > 0 {
		u := queue.pop()
		d := paths[u] + 1
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <bits/stdc++.h>
using namespace std;
#define INFTY (1<<30)

int n;
vector<vector<int>> M(101,vector<int>(101,0));
vector<int> color(101,0),d(101,INFTY);
queue<int> Q;

void bfs(){
    color[1]=1;
    d[1]=0;
    Q.push(1);
    while(!Q.empty()){
        int u=Q.front();
        Q.pop();
        for(int v=1;v<n+1;v++){
            if(M[u][v]&&color[v]==0){
                color[v]=1;
                d[v]=d[u]+1;
                Q.push(v);
            }
        }
        color[u]=2;
    }
}


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

    bfs();

    for(int i=1;i<n+1;i++){
        cout<<i<<' ';
        if(d[i]==INFTY) cout<<-1<<endl;
        else cout<<d[i]<<endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<queue>

using namespace std;
using ll = long long;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  // start-----------------------------------------------
  int n; cin >> n;
  vector<vector<int>> g(n);
  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--;
      g[u].push_back(v);
    }
  }
  vector<int> dist(n, -1);
  queue<int> q;
  q.push(0);
  dist[0] = 0;
  while(!q.empty()){
    int now = q.front();
    q.pop();
    for(int nx: g[now]){
      if(dist[nx] == -1){
        dist[nx] = dist[now] + 1;
        q.push(nx);
      }
    }
  }
  for(int i = 0; i < n; i++) cout << i+1 << ' ' << dist[i] << endl;
  // end-----------------------------------------------
  // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
}

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

void InputSet(std::vector<int> & a) {
	int n;
	cin >> n;
	a.resize(n,0);
	for(int i=0;i<n;++i) {
		cin >> a.at(i);
		a.at(i)--;
	}
}
void BFS(vector<vector<int> > & Ad,
		 vector<int> & V,
		 queue<int> & Q
		 )
{
	while(!Q.empty()) {
		int u = Q.front();
		Q.pop();
		for(int i=0;i<(int)Ad[u].size();++i) {
			int next = Ad[u][i];
			if (V[next] < 0) {
				V[next] = V[u] + 1;
				Q.push(next);
			}
		}
	}
}
int main(){
	int n;
	cin >> n;
	vector<vector<int> > Ad(n);
	vector<int> V(n,-1);
	queue<int> Q;
	for(int k=0;k<n;++k) {
		int u;
		cin >> u;
		--u;
		InputSet(Ad[u]);
	}
	Q.push(0);
	V[0]=0;
	BFS(Ad,V,Q);

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

C++: //
#if 0

#endif // 0
#include<iostream>
#include<queue>
#define MAX 100
using namespace std;
int matri[MAX][MAX];

int main()
{
    int n;
    int r[MAX];
    queue<int> q;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        r[i]=0;
        for(int j=0;j<n;j++)
            matri[i][j]=0;
    }
    for(int i=0;i<n;i++)
    {
        int u,k;
        cin>>u>>k;
        for(int j=0;j<k;j++)
        {
            int m;
            cin>>m;
            matri[u-1][m-1]=1;
        }
    }
    q.push(0);
    r[0]=0;
    while(!q.empty())
    {
        int t;
        t=q.front();
        q.pop();
        for(int i=0;i<n;i++)
            if(matri[t][i]&&r[i]==0&&i)
            {
                q.push(i);
                r[i]=r[t]+1;
               // cout<<r[i]<<endl;
            }
    }
    for(int i=0;i<n;i++)
    {
        if(r[i]||r[i]==i)
            cout<<i+1<<" "<<r[i]<<endl;
        else
            cout<<i+1<<" -1"<<endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int a[101][101],start[101],end[101],n,p=0,v[101];
queue<int> q;
void bfs(int u){
	q.push(u);
	v[u]++;
	while(!q.empty()){
		u=q.front();q.pop();
		r(i,1,n+1)if(!v[i]&&a[u][i]){
			v[i]++;
			q.push(i);
			start[i]=start[u]+1;
		}
	}
}
int main(){
	int q,qq,qqq;
	cin>>n;
	r(i,0,n){
		cin>>q>>qq;
		r(i,0,qq){
			cin>>qqq;
			a[q][qqq]++;
		}
	}
	bfs(1);
	r(i,1,n+1){
		if(i!=1&&start[i]==0)cout<<i<<' '<<-1<<endl;
		else cout<<i<<' '<<start[i]<<endl;
	}
}

C++: #include<iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"
*/

const int WHITE=0, GRAY=1, BLACK=2;
const int INF = 10000;
/*
void bfs(int u, int d[], int color[], int N, int M[][]){
    d[u] = 0;
    color[u] = GRAY;
    queue<int> Q;
    Q.push(u);
    while(!Q.empty()){
        int current_u = Q.front(); Q.pop();
        rep(i,N){
            if(M[current_u][i] && color[i] == WHITE){
                Q.push(i); d[i] = d[u]+1;
            }
        }
    }
    rep(i,N){
        printf("%d %d\n", i+1, d[i]);
    }
}
*/

int main()
{
    int N; cin >> N;
    int d[N], M[N][N], color[N];
    // 初期化
    rep(i, N){
        d[i] = INF;
        color[i] = WHITE;
        rep(j,N) M[i][j] = false;
    }
    // 辺の情報を読み込む
    rep(i,N){
        int id; cin >> id;
        int k; cin >> k;
        rep(j,k){
            int vj; cin >> vj;
            M[i][vj-1] = true;
        }
    }
    d[0] = 0;
    color[0] = GRAY;
    queue<int> Q;
    Q.push(0);
    while(!Q.empty()){
        int current_u = Q.front(); Q.pop();
        // cout << current_u << endl;
        rep(i,N){
            if(M[current_u][i] && color[i] == WHITE){
                Q.push(i); d[i] = d[current_u]+1;
                color[i] = GRAY;
            }
        }
        color[current_u] = BLACK;
    }
    rep(i,N){
        if(d[i] == INF) printf("%d %d\n", i+1, -1);
        else printf("%d %d\n", i+1, d[i]);
    }
}

C++: #include<cstdio>
#include<queue>
#include<iostream>
using namespace std;
int n, visited[101], d[101];
int adj[101][101];
queue<int> q;
void bfs(int u)
{
  if(visited[u]) return;
  q.push(u);
  visited[u] = 1;
  while(!q.empty())
  {
    int v = q.front();
    for(int i=1; i<=n; i++)
    {
      if(adj[v][i] && visited[i]==0)
      {
        q.push(i);
        visited[i] = 1;
        d[i] = d[v] + 1;
      }
    }
    q.pop();
  }
}
int main()
{
  scanf("%d", &n);
  for(int i=0; i<=n; i++)
  {
    for(int j=0; j<=n; j++)
      adj[i][j] = 0;
    visited[i] = 0;
    d[i] = -1;
  }
  d[1] = 0;
  int u, k, v;
  for(int i=0; i<n; i++)
  {
    scanf("%d%d", &u, &k);
    for(int j=0; j<k; j++)
    {
      scanf("%d", &v);
      adj[u][v] = 1;
    }
  }
//  for(int i=1; i<=n; i++)
//  {
//    if(visited[i] == 0)
//      bfs(i);
//  }
  bfs(1);
  for(int i=1; i<=n; i++)
  {
    printf("%d %d\n", i, d[i]);
  }


  return 0;
}