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 <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>

using namespace std;

deque<int> to_visit;
int t = 1;

int main() {
  int n;
  cin >> n;

  vector<bool> visited(n + 1, false);
  vector<int> dist(n + 1, -1);
  vector<vector<int>> V(n + 1);

  cin.ignore();

  for (string line; getline(cin, line);) {
    istringstream is{line};
    int id, n_nbrs, nbr;
    is >> id >> n_nbrs;
    vector<int> out_nbrs;
    while (is >> nbr)
      out_nbrs.push_back(nbr);
    V[id] = out_nbrs;
  }

  to_visit.push_back(1);
  dist[1] = 0;
  while (!to_visit.empty()) {
    if (visited[to_visit.front()]) {
      to_visit.pop_front();
      continue;
    }
    int curr = to_visit.front();
    to_visit.pop_front();
    visited[curr] = true;
    for (auto nbr : V[curr]) {
      if (dist[nbr] == -1)
        dist[nbr] = dist[curr] + 1;
      to_visit.push_back(nbr);
    }
  }

  for (int i = 1; i < V.size(); ++i) {
    cout << i << " " << dist[i] << endl;
  }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n;
  cin >> n;
  vector<vector<int>> Garph(n);
  rep(i, n) {
    int u, k;
    cin >> u >> k;
    rep(j, k) {
      int v;
      cin >> v;
      Garph[u-1].push_back(v-1);
    }
  }
  vector<int> dist(n, -1);
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  while(!que.empty()) {
    int v = que.front();
    que.pop();
    for(auto next_v : Garph[v]) {
      if(dist[next_v] != -1) { continue;}
      dist[next_v] = dist[v] + 1;
      que.push(next_v);
    }
  }
  for(int i = 0; i < n; i++) {
    printf("%d %d\n", i+1, dist[i]);
  }
  return 0;
}

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

#define MAX 100
int n;
int M[MAX][MAX];
int dist[MAX];

void bfs(int);

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;
    cin >> k;
    u--;
    for( i = 0; i < n; i++){
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);
  return 0;
}

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

C++: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
int n,v[105],d[105];
vector<int>m[105];
void bfs(int s)
{
    v[s]=true;
    d[s]=0;
    queue<int>q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();q.pop();
        for(auto i:m[u])
        {
            int vv=i;
             if(!v[vv])
            {
                d[i]=d[u]+1;
                v[i]=true;
                q.push(i);
            }
        }
    }
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)d[i]=-1;
    for(int i=1;i<=n;i++)
    {
        int u,v,k;
        cin>>u>>k;
        for(int j=0;j<k;j++)
        {
            cin>>v;
            m[u-1].push_back(v-1);
        }
    }
    bfs(0);
    for(int i=0;i<n;i++)cout<<i+1<<' '<<d[i]<<endl;
    return 0;
}

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

#define MAX 100
#define Q_MAX 50
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY -1

// d[i]は始点sから各頂点iまでの最短距離
int M[MAX][MAX], color[MAX], d[MAX], Q[Q_MAX], head, tail;

void enqueue(int x){
  //if(x == 18) cout << "head: " << head << ", tail: " << tail << endl;
  Q[tail] = x;
  if(tail + 1 == Q_MAX) tail = 0;
  else tail++;
}

int dequeue(){
  int x = Q[head];
  if(head + 1 == Q_MAX) head = 0;
  else head++;
  return x;
}

// 頂点番号は0-オリジン
void bfs(int n, int s){
  // 初期化
  head = 0;
  tail = 0;
  for(int i=0; i<n; i++){
    color[i] = WHITE;
    d[i] = INFTY;
  }

  color[s] = GRAY;
  d[s] = 0;
  enqueue(s);

  int u;
  while(head != tail){
    u = dequeue();
    //cout << "u: " << u+1 << endl;
    for(int v=0; v<n; v++){
      if(M[u][v] && color[v] == WHITE){
        color[v] = GRAY;
        d[v] = d[u] + 1;
        //cout << "v: " << v+1 << endl;
        enqueue(v);
      } 
    }
    color[u] = BLACK;
  }
}

int main(void){
  int n, u, k, v;
  cin >> n;

  // M[][]の初期化
  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--; // 頂点番号を0-オリジンにする。
    for(int j =0; j<k; j++){
      cin >> v;
      M[u][--v] = 1; // 頂点番号を0-オリジンにする。
    } 
  }

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