Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

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

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", vertices[i].id, vertices[i].visitedAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: #include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
#include <utility>
#define rep(i,n) for(int i=0; i<(n); ++i)
#define reps(i, s, n) for(int i=(s); i<=(n); ++i)
#define outl(x) cout<< (x) << '\n'
#define fi first
#define se second
#define mkp make_pair

#define INF 0x3f3f3f3f
#define NIL -1
using namespace std;

int N;
vector <int> G[102];
int far[102];

void slv()
{
   queue <int> que;
   que.push(1);
   memset( far, NIL, sizeof(far) );
   far[1] = 0;

   while(!que.empty()){
      const int u = que.front();
      que.pop();
      rep(i, G[u].size()){
         const int next = G[u][i];
         if( far[next] == NIL ){
            far[next] = far[u] + 1;
            que.push( next );
         }
      }
   }

   reps(i, 1, N)
      cout << i <<  ' ' <<  far[i]  << '\n';
   return;
}

signed main()
{
   cin.tie(0), ios::sync_with_stdio(false);
   cin >> N;

   reps(i, 1, N){
      int u, k, a;
      cin >> u >> k;
      while(k--){
         cin >> a;
         G[u].push_back(a);
      }
   }

   slv();
   return (0);
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;

int main(){
  int n;
  cin >> n;
  vector<vector<int>> gr(n, vector<int>());
  rep(i, n){
    int u, k;
    cin >> u >> k;
    u--;
    rep(j, k){
      int a;
      cin >> a;
      a--;
      gr.at(u).push_back(a);
    }
  }
  vector<int> ans(n, -1);
  ans.at(0) = 0;
  queue<int> q;
  q.push(0);
  while(!q.empty()){
    int now = q.front(); q.pop();
    for(auto i : gr.at(now)){
      if(ans.at(i) == -1){
        ans.at(i) = ans.at(now)+1;
        q.push(i);
      }
    }
  }
  rep(i, n){
    cout << i+1 << " " << ans.at(i) << endl;
  }
  return 0;
}