Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
type Queue struct {
	arr []int
}

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

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

func (q *Queue) isEmpty() bool {
	return len(q.arr) == 0
}

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		u, k := sc.nextInt()-1, sc.nextInt()
		for j := 0; j < k; j++ {
			G[u] = append(G[u], sc.nextInt()-1)
		}
	}
	var q Queue
	seen := make([]int, N)
	for i := 0; i < N; i++ {
		seen[i] = -1
	}
	var search func(cur int)
	search = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if seen[next] == -1 {
				q.push(next)
				seen[next] = seen[cur] + 1
			}
		}
	}
	q.push(0)
	seen[0] = 0
	for !q.isEmpty() {
		search(q.pop())
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i+1, seen[i])
	}
	wtr.Flush()
}

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

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

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

  return 0;
}

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

using namespace std;

int g[102][102];
int d[102];
int q[102];
int t = 0, n, p = 0;

int bfs(int x){
    for(int i=0; i<=n; i++){
        
    }
    return 0;
}
int main(){
    cin >> n;

    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            g[i][j] = 0;
        }
        d[i] = -1;
    }
    
    int u,k;
    for(int i=0; i<n; i++){
        cin>>u>>k;
        for(int j=0; j<k; j++){
            int v;
            cin>>v;
            g[u][v] = 1;
        }
    }
    //   for(int i=1; i<=n; i++){
    //  if(d[i] != -1) continue;

    q[p++] = 1;
    d[1] = 0;
    while(p > -1){
        u = q[p--];
        for(int i=1; i<=n; i++){
            if(g[u][i] == 1){
                g[u][i] = 0;
                d[i] = d[u]+1; 
                q[p++] = i;
            }
        }
    }
    

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

C++: //
//  main.cpp
//
// g++ -std=c++14 main.cpp
//

//#include "main.hpp"
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <math.h>
#include <queue>
#include <tuple>
using namespace std;
typedef long long ll;

int main(void){
    int n;
    cin >> n;
    
    vector<int> v(n, -1);
    int a[105][105] = {};
    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            a[u - 1][v - 1] = true;
        }
    }
    
    queue<int> que;
    int num[105] = {}, idx = 0;
    que.push(0);
    v[0] = 0;
    num[0] = 1;
    
    bool at[105] = {};
    at[0] = true;
    while (!que.empty()) {
        int q = que.front();
        for (int j = 0; j < n; j++) {
            if (a[q][j] && !at[j]) {
                at[j] = true;
                que.push(j);
                v[j] = idx + 1;
                num[idx + 1]++;
            }
        }
        num[idx]--;
        if (!num[idx]) idx++;
        que.pop();
    }
    
    for (int i = 0; i < n; i++) cout << i + 1 << " " << v[i] << endl;
    
}
// g++ -std=c++14 main.cpp

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <utility>
#include <ctime>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007
 
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=(n);i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define vector_rsort(x) sort(all(x));reverse(all(x))
#define array_rsort(x,n) sort(x,x+n);reverse(x,x+n);
#define pb push_back
#define mp make_pair
#define MOD(x) (x%(mod))

#define Yes cout<<"Yes"<<endl
#define YES cout<<"YES"<<endl
#define No cout<<"No"<<endl
#define NO cout<<"NO"<<endl
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;
 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;
/*---------------------------------------------------*/

int n,u,k,v;
int g[105][105];

void init(){
  rep(i,105){
    rep(j,105){
      g[i][j]=INF;
    }
  }
}

void wf(){
  rep(k,105){
    rep(i,105){
      rep(j,105){
	g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
      }
    }
  }
}

int main(){
  init();
  cin>>n;
  rep(i,n){
    cin>>u>>k;
    rep(i,k){
      cin>>v;
      g[u][v]=1;
    }
  }
  wf();
  for(int i=1;i<=n;i++){
    if(g[1][i]==INF)cout<<i<<" "<<0<<endl;
    else cout<<i<<" "<<g[1][i]<<endl;
  }
  return 0;
}

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

int main(void){
  int N;
  cin >> N;
  
  vector<vector<int>> G(N);
  for(int i=0;i<N;i++){
    int u,k;
    cin >> u >> k;
    for(int j=0;j<k;j++){
      int tmp;
      cin >> tmp;
      G[i].push_back(tmp-1);
    }
  }
  
  vector<int> S(N);
  for(int i=0;i<N;i++){
    S[i] = -1;
  }
  queue<pair<int,int>> Q;
  for(auto it:G[0]){
    Q.push(make_pair(it,1));
  }
  S[0] = 0;
  while(!Q.empty()) {
    auto i = Q.front();
    Q.pop();
    if(S[i.first]>-1){
      continue;
    } else {
      S[i.first] = i.second;
    }
      
    for(auto it: G[i.first]){
      Q.push(make_pair(it,i.second+1));
    }
  }
  
  for(int i=0;i<N;i++){
    cout << i+1 << " " << S[i] << endl;
  }
}