Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

C++: using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define divup(a,b) ( ( (a) + ( (b) - 1u) ) / (b) ) // a/b の余り切り上げ
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
const ll mod = 1000000007;
const ll mod9 = 1000000009;

ll d[109];
ll u[109];
ll k[109];
ll v[109][109];
struct Point{
    ll s,t;
};
queue<Point> S;

ll  bfs(){
    while(!S.empty()){
        ll n = S.front().s;
        ll t = S.front().t;
        S.pop();
        if(d[n]==0){
            d[n]=t;
        // cout<<" d "<<n<<" "<<t<<endl;
        }
        rep(i,k[n]){
            if(d[v[n][i]-1]==0) S.push({v[n][i]-1,t+1});
        }
    }
    return 0;
}

signed main(){
    ll n;
    cin>>n;
    rep(i,n){
        ll a;
        cin>>u[i]>>k[i];
        rep(j,k[i]){
            cin>>v[i][j];
        }
    }
    S.push({0,1});
    bfs();
    rep(i,n){
        d[i]--;
        printf("%lld %lld\n",i+1,d[i]);
    }
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct id{
    int c[101];
    int p=0;
    int d=-1;
    int u=0;
};
int n,t=0;
id a[101];
queue<int> q;
void bps(int e){
    q.push(e);
    while(!q.empty()){
        int f=q.front();
        for(int i=1;i<=n;i++){
            if(a[f].c[i]==1&&a[i].u==0){
                a[i].d=a[f].d+1;
                q.push(i);
                a[i].u++;
            }
        }
        q.pop();
        a[f].u++;
    }
    
}

int main(){
    int x,y,z;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        for(int j=1;j<=y;j++){
            cin>>z;
            a[x].c[z]=1;
            a[z].p=x;
        }
    }
    a[1].d+=1;
    bps(1);
    for(int i=1;i<=n;i++){
        cout<<i<<" "<<a[i].d<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <queue>
#define INFTY -10000000
using namespace std;



int a[124][124] = {{}};
int d[124] = {};
int n;
void bfs(int k){
  queue <int> q;
  q.push(k);
  int u;


  while(!q.empty()){
    u = q.front(); q.pop();
    for(int v = 0;v < n;v++){
      if(a[u][v] == 0) continue;
      if(d[v] == INFTY) {
        d[v] = d[u] + 1;
        cout << v << " " << d[v] << endl;
      }
      q.push(v);
    }

  }
}

int main(){

  int u,k,x;
  cin >> n;

d[0] = 0;
for(int i = 1; i < n;i++) d[i] = INFTY;

  for(int i = 0;i < n;i++){
    cin >> u >> k;
    for(int j = 0;j < k;j++){
      cin >> x;
      a[u - 1][x - 1] = 1;
    }
  }

  bfs(0);

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

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

int main(){

  bool adj[100][100]={};//初期化を忘れない
  int d[100]={};
  int n,u,v,k;

  cin >> n;
  for (int i=0; i<n; i++){
    cin >> u >> k;
    for (int j=0; j<k; j++){
      cin >> v;
      adj[u-1][v-1] = true;
    }
  }
  
  queue<int> q;
  bool pushed[100]={};
  q.push(1);
  pushed[1-1] = true;
  d[0] = 0;

  while(q.empty() == false){
    u = q.front();
    q.pop();
    for (int i=0; i<n; i++){
      if (adj[u-1][i]){
	if (pushed[i] != true){
	  d[i] = d[u-1] + 1;
	  q.push(i+1);
	  pushed[i] = true;
	}
      }
    }
  }
  
  cout << 1 << " " << 0 << endl;
  for (int i=1; i<n; i++){
    if (d[i] == 0)
      cout << i+1 << " " << -1 << endl;
    else
      cout << i+1 << " " << d[i] << endl;      
  }

}

C++: #include<bits/stdc++.h>
#define ll long long
#define rep(i, n) for(int i=0;i<(n);++i)
#define per(i, n) for(int i=(n)-1;i>=0;--i)
#define repa(i, n) for(int i=1;i<(n);++i)
#define foreach(i, n) for(auto &i:(n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
const ll MOD = (ll)1e9+7;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
using namespace std;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}

ll modpow(ll x, ll b){
	ll res = 1;
	while(b){
		if(b&1)res = res * x % MOD;
		x = x * x % MOD;
		b>>=1;
	}
	return res;
}

ll modinv(ll x){
	return modpow(x, MOD-2);
}

bool was_output = false;
template<class t>
void output(t a){
	if(was_output)cout << " ";
	cout << a;
	was_output = true;
}
void outendl(){
	was_output = false;
	cout << endl;
}

vvector<int> edges;
vector<int> found_time;

void bfs(int start){
	found_time[start] = 0;
	queue<pii> que;
	que.emplace(start, 0);
	while(que.size()){
		pii d = que.front();
		que.pop();
		foreach(i, edges[d.first]){
			if(found_time[i]>=0)continue;
			found_time[i]=d.second+1;
			que.emplace(i, d.second+1);
		}
	}
}

int main(){
	int n;
	cin >> n;
	edges.resize(n);
	found_time.resize(n, -1);
	rep(i, n){
		int t;
		int time;
		cin >> t >> time;
		--t;
		rep(j, time){
			int to;
			cin >> to;
			--to;
			edges[t].push_back(to);
		}
	}
	bfs(0);
	rep(i, n){
		output(i+1);
		output(found_time[i]);
		outendl();
	}
	return 0;
}