Go: package main

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

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ds = append(ds, -1)
	}

	q := make([]int, 0, n)
	q = append(q, 0)
	ds[0] = 0

	for len(q) > 0 {
		i := pop(&q)
		for _, v := range vecs[i] {
			if ds[v] == -1 {
				ds[v] = ds[i]+1
				q = append(q, v)
			}
		}
	}

	for i, d := range ds {
		fmt.Fprintln(w, i+1, d)
	}

	w.Flush()
}

func pop(q *[]int) int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

C++: #include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <deque>
#include <assert.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <math.h>
#include <bitset>
#include <iomanip>
#include <complex>
#include <ios>
#include <iomanip>
using namespace std;
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define IDX(vec, element_iter) distance((vec).begin(), element_iter)
#define print(x) cout<<(x)<<'\n'
#define print_vec(v) rep(i,v.size()){if(i!=0)cout<<" ";cout<<v[i];}cout<<'\n'
#define pe(x) cout<<(x)<<" "
#define DEBUG(x) cout<<#x<<": "<<x<<'\n'
#define DEBUG_VEC(v) cout<<#v<<":";rep(i,v.size())cout<<" "<<v[i];cout<<'\n'
#define pb push_back
#define mp make_pair
#define MOD 1000000007 //10^9+7
#define fi first
#define sc second
using ll=long long;
using pi=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
using vstr=vector<string>;

void solve() {
    int n; cin >> n;
    vector<vi> to(n);
    int u,k,v;
    rep(i,n){        
        cin >> u >> k;
        u--;
        rep(j,k){
            cin >> v;
            to[u].pb(--v);
        }
        // DEBUG_VEC(to[u]);
    }

    vi dist(n,-1);
    dist[0] = 0;
    queue<int> que;
    que.push(0);
    while(!que.empty()){
        int root = que.front();
        que.pop();
        // DEBUG(root);
        // DEBUG_VEC(to[root]);
        for(auto leaf:to[root]){
            if(dist[leaf] != -1) continue;
            dist[leaf] = dist[root]+1;
            que.push(leaf);
        }
    }

    rep(i,n){
        cout << i+1 << " " << dist[i] << '\n';
    }
}
 
int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
	cout<<fixed<<setprecision(20);
    //int q; cin >> q;
	//while (q--)
		solve();
    return 0;
}

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

const int Max = 100;
const int white = 0;
const int black = 1;
int M[Max][Max];
int d[Max], nt[Max], color[Max];
int n;
queue<int> Q;

int next(int u){
    for(int v=nt[u]; v<n; v++){
        nt[u] = v + 1;
        if(M[u][v]) return v;
    }
    return -1;
}

void bfs_visit(int u){
    for(int i=0; i<n; i++){
        nt[i] = 0;
        d[u] = -1;
    }

    color[u] = black;
    d[u] = 0;
    Q.push(u);
    while(!Q.empty()){
        int u = Q.front();
        int v = next(u);
        if(v != -1){
            if(color[v] == white){
                color[v] = black;
                d[v] = d[u] + 1;
                Q.push(v);
            }
        } else {
            Q.pop();
        }
    }
}

int main(){
    int u, k, v;
    scanf("%d", &n);
    for(int i=0; i<n; i++){
        color[i] = white;
    }
    for(int i=0; i<n; i++){
        cin >> u >> k;
        u--;
        for(int i=0; i<k; i++){
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs_visit(0);

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

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;
}