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>
//#define int long long
//#define _DEBUG
#include <queue>
#include <vector>
using namespace std;
int N, T[107][107], D[107]; //T[i][j]:i->T[i][j]の辺
int const infty = -1, none = -9;
queue<int> Q;
void solve(){
    Q.push(0);
    D[0] = 0;
    while(true){
#ifdef _DEBUG
        cout << "Q1 ";
        int length = Q.size();
        for(int k=0; k<length; k++){
            int qf = Q.front(); Q.pop();
            cout << qf << " ";
            Q.push(qf);
        }cout << endl; 
#endif
        int top = Q.front(); Q.pop();
#ifdef _DEBUG
        cout << "top " << top << endl << "Q2 ";
        length = Q.size();
        for(int k=0; k<length; k++){
            int qf = Q.front(); Q.pop();
            cout << qf << " ";
            Q.push(qf);
        }cout << endl; 
#endif
        for(int j=0; j<N; j++){
#ifdef _DEBUG
            cout << j << ", " << T[top][j] << endl;
            cout << "top " << top << endl << "Q3 ";
            length = Q.size();
            for(int k=0; k<length; k++){
                int qf = Q.front(); Q.pop();
                cout << qf << " ";
                Q.push(qf);
            }cout << endl; 
#endif
            if(T[top][j] == none)break;
            if(D[ T[top][j] ] != infty)continue;
            Q.push( T[top][j] );
            D[ T[top][j] ] = D[top]+1;
        }
#ifdef _DEBUG
        cout << "D " ;
        for(int k=0; k<N; k++) cout <<D[k]<<" ";
        cout << endl;
#endif
        if(Q.empty()) break;
    }
}
void get(){
    cin >> N;
    for(int i=0; i<N; i++){
        D[i] = infty;
        for(int j=0; j<N; j++){
            T[i][j] = none;
        }
    }
    for(int i=0; i<N; i++){
        int i_, k;
        cin >> i_ >> k;
        for(int j=0; j<k; j++){
            int vj;
            cin >> vj;
            T[i][j] = vj-1;
        }
    }
}

signed main(){
    get();
    
#ifdef _DEBUG
    for(int k=0; k<N; k++) cout << D[k] << " ";
    cout << endl;
    for(int k=0; k<N; k++){
        for(int l=0; l<N; l++){
            if(T[k][l] == none)break;
            cout << T[k][l] << " ";
        }
        cout << endl;
    }
#endif
    solve();
    for(int i=0; i<N; i++){
        cout << i+1 << " " << D[i] << endl;
    }
}

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

using namespace std;

int D[100100];//始点からの距離を入れる

int g[1001][1001]={{0}};//要素を0で初期化
void bfs(int src,int n){//srcは開始位置この場合だと頂点1だから0
  // cerr << "bfs_root = " << src << endl;
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while (! Q.empty()  ){
    int cur = Q.front();
    Q.pop();
    // cerr << "visiting_" << cur << " " <<D[cur] <<endl;
    for(int dst= 0; dst < n; dst++){
      if(g[cur][dst]==1 && D[dst]==0){
        D[dst]=D[cur]+1;
        Q.push(dst);
      }
    }
  }
}
void ans(int n){
  cout << 1 <<" "<< 0 <<enl;
  for (int i = 1; i < n; ++i)
  {
    if(D[i]==0){
      cout << i+1 << " "<< -1 <<endl;
    }else{
      cout << i+1 <<" "<< D[i]<<endl;
    }
  }

}

int main(){
  // int g[1001][1001]={{0}};//要素を0で初期化
  int n,a,b,c;
  cin >> n;
  for (int i = 0; i < n; ++i)
  {
    cin >> a >> b ;
    for (int i = 0; i < b; ++i)
    {
      cin >> c;
      g[a-1][c-1]=1;/* code */
    }
  }
  bfs(0,n);
  ans(n);
  // for (int i = 0; i < n; ++i)
  // {
  //    for (int j = 0; j < n; ++j)
  //    {
  //     if (j==n-1)
  //     {
  //       cout << g[i][j] << endl;
  //     }else{
  //       cout << g[i][j] <<" ";
  //     }
  //    }
  // }


}

C++: #include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<vector<int>> edges(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--;
            edges[u].push_back(v);
        }
    }

    vector<int> ds(n, -1);
    ds[0] = 0;
    queue<pair<int, int>> q;
    q.push(make_pair(0, 0));
    while (!q.empty()) {
        int u = q.front().first;
        int d = q.front().second;
        q.pop();
        if (ds[u] > 0) {
            continue;
        }
        ds[u] = d;
        for (auto v : edges[u]) {
            q.push(make_pair(v, d + 1));
        }
    }

    for (int i = 0; i < n; i++) {
        cout << i + 1 << " " << ds[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;
}