Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 0
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for (int (i) = 0; (i) < (int)(n); i++) 
#define FOR(i, a, b) for(int (i) = a; (i) < (int)b; i++)
#define RREP(i, n) for(int (i)=((int)(n)-1); (i)>=0; i--)
#define RFOR(i, a, b) for(int (i) =((int)(b)-1); (i)>=(int)a; i--)
#define ALL(v) (v).begin(),(v).end()
#define MOD 1000000007
#define NIL -1
#define FI first
#define SE second
#define MP make_pair
#define PB push_back
#define SZ(x)  (int)x.size()
#define SP(x)  setprecision((int)x)


using namespace std ;
typedef long long ll;
using Graph = vector<vector<int>>;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef vector<string> vstr;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll LINF = 1e18;
const double EPS = 1e-9;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} //最大公約数
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} //最小公倍数

//-------------------------------------------------
// 
void yes(){
  cout <<"Yes"<<endl ;
}
void no(){
  cout <<"No"<<endl ;
}


//-------------------------------------------------
// メモ
/*






*/
//-------------------------------------------------


int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n ;
  cin >> n ;
  vint d(n,-1) ;
  vint seen(n,0) ;
  queue<int> Q ;
  vvint gra(n) ;
  for(int i=0;i<n;i++){
    int a, b;
    cin >> a >> b;
    for(int j=0;j<b;j++){
      int c ;
      cin >> c ;
      gra[a-1].PB(c-1) ;
    }
  }
  
  Q.push(0) ;
  seen[0] = 1 ;
  d[0] = 0 ;
  
  while(!Q.empty()){
    int k = Q.front() ;
    for(int i=0;i<SZ(gra[k]);i++){
      if(seen[gra[k][i]]==0){
        Q.push(gra[k][i]) ;
        seen[gra[k][i]] = 1 ;
        d[gra[k][i]] = d[k] + 1 ;
      }
    }
    Q.pop() ;
  }
  
  for(int i=0;i<n;i++){
    cout << i+1 <<" "<<d[i] <<endl ;
  }
 
  return 0 ;
}

C++: #include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;

int *d;
int *k;
int **a;

void bfs(){
    int i, v, u;
    queue<int> q;
    q.push(1);
    d[1] = 0;
    while(!q.empty()){
        u = q.front();
        q.pop();
        for(i=0;i<k[u];i++){
            v = a[u][i];
            if(d[v]<0){
                q.push(v);
                d[v] = d[u] + 1;
            }
        }
    }
}

int main(){
    int i, j, n, u, v;
    scanf("%d",&n);
    d = (int *)malloc(sizeof(int)*(n+1));
    k = (int *)malloc(sizeof(int)*(n+1));
    a = (int **)malloc(sizeof(int *)*(n+1));
    for(i=0;i<=n;i++)d[i] = -1;

    for(i=0;i<n;i++){
        scanf("%d",&u);
        scanf("%d",k+u);
        a[u] = (int *)malloc(sizeof(int)*k[u]);
        for(j=0;j<k[u];j++){
            scanf("%d",&v);
            a[u][j] = v;
        }
    }

    bfs();

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

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define MAX 100
#define Black 1
#define White 0

int f[MAX];
int adj[MAX][MAX];
int n;

void bfs(int r){
    rep(i,n)f[i]=-1;
    queue<int> Q;
    Q.push(r);
    f[r]=0;
    while(!Q.empty()){
        int u=Q.front();Q.pop();
        rep(i,n){
            if(adj[u][i]==1&&f[i]==-1){
                f[i]=f[u]+1;
                Q.push(i);
            }
        }
    }
    rep(i,n){
        printf("%d %d\n",i+1,f[i]);
    }
}

int main(){
    int id,k,node;
    cin>>n;
    rep(i,n)rep(j,n)adj[i][j]=0;
    rep(i,n){
        scanf("%d %d",&id,&k);
        id--;
        rep(j,k){
            scanf("%d",&node);
            node--;
            adj[id][node]=1;
        }
    }
    bfs(0);
    return 0;
}

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

struct edge {int to, cost;};
//=============dijkstra============================
vector<long long> dijkstra(int s, int V, vector<vector<edge>> G)
{
  vector<long long> d(V, INF);
  typedef pair<long long, int> P;// first は最短距離　second は頂点の番号
  priority_queue<P, vector<P>, greater<P>> que;
  d.at(s) = 0;
  que.push(P(0, s));
  while (!que.empty())
  {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d.at(v) < p.first) continue;
    for (int i = 0; i < G.at(v).size(); ++i)
    {
      edge e = G.at(v).at(i);
      if (d.at(e.to) > d.at(v) + e.cost)
      {
        d.at(e.to) = d.at(v) + e.cost;
        que.push(P(d.at(e.to), e.to));
      }
    }
  }
  return d;
}
//=================================================


int main()
{
  int n;
  cin >> n;
  vector<vector<edge>> G(n, vector<edge>(0));
  for (int i = 0; i < n; ++i)
  {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; ++j)
    {
      int v;
      cin >> v;
      G.at(u - 1).push_back({v - 1, 1});
    }
  }
  vector<long long> ans = dijkstra(0, n, G);
  for (int i = 0; i < n; ++i)
  {
    if (ans.at(i) == INF)
    {
      cout << i + 1 << ' ' << -1 << endl;
    }
    else
    {
      cout << i + 1 << ' ' << ans.at(i) << endl;
    }
  }
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<iostream>
#include<sstream>
#include<utility>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<stack>

using namespace std;

const int MAX_N = 101;
const int DUMMY = 10000;

int N, u, k, v = 0;

vector<int>V [MAX_N];
int d [MAX_N];
bool visited [MAX_N];

bool bfs(int u, int depth)
{
    //if (V[u].size() == 0) return false;
    
    // for (int i = 0; i < V[u].size(); ++i)
    //     cout << V[u][i] << ' ';
    // cout << endl;
    
    for (int i = 0; i < V[u].size(); ++i)
        bfs(V[u][i] - 1, depth + 1);
    
    d[u] = min(d[u], depth);
    return true;
}

int main()
{
    //initialize
    for (int i = 0; i < MAX_N; ++i) {
        d[i]   = DUMMY;
        visited[i] = false;
    }
    
    //input
    scanf("%d", &N);
    for (int i = 0; i < N; ++i) {
      scanf("%d %d", &u, &k);
      for (int j = 0; j < k; ++j) {
          scanf("%d", &v);
          V[i].push_back(v);
      }
      sort(V[i].begin(), V[i].end());
    }

    //serach
    bfs(1-1, 0);
    
    //output
    for (int i = 0; i < N; ++i) {
      if(d[i] == DUMMY) cout << i+1 << ' ' << -1 << endl;
      else cout << i+1 << ' ' << d[i] << endl;
    }

    return 0;
}