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++: #if 0
?????¨?????????1.5h(???????????????)
bfs??????????????¢?´¢???????????°????????§?????????
????????????????´????????????\?????????????????????????????¨????????????????????£??????????????????queue?????????????????¢?????????
main?????§???????????§?§£????????£??\?????????to??£??\??????????????????????????£??????
????????§?????????G????????????cout??§??????(presentation error)?????¨???????????´?????????
#endif

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int n, D[100];
int G[100][100];
void bfs(int src){
	queue<int> q;
	q.push(src);
	D[src]=0;
	while(!q.empty()){
		int cur=q.front();
		q.pop();
		for(int dst=0;dst<n;dst++){
			if(G[cur][dst]==1&&D[dst]==-1){
				D[dst]=D[cur]+1;
				q.push(dst);
			}
		}
	}
};

int main()
{
	vector<int> v[110];
	cin>>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;
			v[u].push_back(tmp);
		}
	}
	for(int i=0;i<100;i++){
		D[i]=-1;
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			G[i][j]=0;
		}
	}
	for(int i=1;i<n+1;i++){
		for(int j=0;j<v[i].size();j++){
			G[i-1][v[i][j]-1]=1; //
		}
	}
	bfs(0);
	for(int i=0;i<n;i++){
		cout<<(i+1)<<' '<<D[i]<<endl;
	}
}

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

using namespace std;

using ll = long long;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)

#define input(...) __VA_ARGS__; in(__VA_ARGS__)

template<class T>
void print(vector<T> a) {
  cout << "[ ";
  REP(i, a.size()) cout << a[i] << " ";
  cout << "]" << endl;
}

void print() {
  std::cout << std::endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...);
}

void in() { }

template <class Head, class... Tail>
void in(Head&& head, Tail&&... tail) {
  cin >> head;
  in(std::forward<Tail>(tail)...);
}

int main() {
  ll input(n);
  vector<vector<ll>> graph(n);
  REP(i, n) {
    ll input(u, k);
    REP(j, k) {
      ll input(v);
      graph[u - 1].push_back(v - 1);
    }
  }

  vector<bool> visited(n);
  vector<ll> cost(n);
  queue<pair<ll, ll>> que;
  que.push({0, 0});
  while (!que.empty()) {
    pair<ll, ll> node = que.front();
    que.pop();

    if (!visited[node.first]) {
      cost[node.first] = node.second;
    }
    visited[node.first] = true;

    for(auto v: graph[node.first]) {
      if (!visited[v]) {
        que.push({v, node.second + 1});
      }
    }
  }
  REP(i, n) {
    if (!visited[i]) cost[i] = -1;
    cout << i + 1 << " " << cost[i] << endl;;
  }
}

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

using namespace std;

int d[100];
vector<int> adj[100];

void bfs(int n, int start) {
	queue<int> que;

	for (int i = 0; i < n; i++)
		d[i] = (i == start ? 0 : -1);

	que.push(start);
	while(!que.empty()) {
		int from = que.front();
		que.pop();

		for (int i = 0; i < adj[from].size(); i++) {
			int to = adj[from][i];
			if (d[to] == -1) {
				d[to] = d[from] + 1;
				que.push(to);
			}
		}
	}
}

int main() {
	int n;

	cin >> n;

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		while (k--) {
			int v;
			cin >> v;
			adj[u - 1].push_back(v - 1);
		}
	}

	bfs(n, 0);

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

	return 0;
}

C++: #include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

#define MAX_N (105)

int n;
bool G[MAX_N][MAX_N];
int dist[MAX_N];
int main()
{
    queue<int> q;
    scanf("%d", &n);
    for ( int i = 1; i <= n; i++ ) {
	int u, k;
	scanf("%d %d", &u, &k);
	for ( int j = 0; j < k; j++ ) {
	    int d;
	    scanf("%d", &d);
	    G[u][d] = true;
	}
    }

    memset(dist, -1, (n+1) * sizeof(int));

    dist[1] = 0;
    q.push(1);
    
    while ( !q.empty() ) {
	int cur = q.front(); q.pop();
	for ( int i = 1; i <= n; i++ ) {
	    if ( G[cur][i] && (dist[i] == -1) ) {
		dist[i] = dist[cur] + 1;
		q.push(i);
	    }
	}
    }

    for ( int i = 1; i <= n; i++ ) {
	printf("%d %d\n", i, dist[i]);
    }

    return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;
#define NIL -2000000001
#define MAX 101
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n = 0;
int adl[MAX][MAX], deg[MAX];
int adm[MAX][MAX];
int color[MAX];
int d[MAX];
queue<int> queueBfs;


void bfs() {
  for ( int i = 0; i < n; i++ ) {
    color[i] = WHITE;
    d[i] = -1;
  }
  queueBfs.push(0);
  color[0] = GRAY;
  d[0] = 0;

  while (!queueBfs.empty()) {
    int top = queueBfs.front();

    for (int i = 0; i < n; i++) {
      if (adm[top][i] == 1 && color[i] == WHITE) {
        queueBfs.push(i);
        color[i] = GRAY;
        d[i] = d[top]+1;
      }
    }

    queueBfs.pop();
  }
}

int main() {
  scanf("%d", &n);
  for ( int i = 0; i < n; i++ ) {
    for ( int j = 0; j < n; j++ ) {
      adm[i][j] = 0;
    }
  }
  for ( int i = 0; i < n; i++ ) {
    int num;
    scanf("%d", &num);
    num--;
    scanf("%d", &deg[num]);
    for ( int j = 0; j < deg[num]; j++) {
      scanf("%d", &adl[num][j]);
      adl[num][j]--;
      adm[num][adl[num][j]] = 1;
    }
  }

  bfs();

  for ( int i = 0; i < n; i++ ) {
    printf("%d %d\n", i+1, d[i]);
  }



  return 0;
}