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 <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a), i##_len=(n); i>i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 101;
constexpr int INF = (1<<21);
int n;
vv(int, M, MAX, MAX, 0);
vi d(MAX, INF);
inline void bfs() {
	d[1] = 0;
	deque<int> dq;
	dq.eb(1);
	int u;
	while(!dq.empty()) {
		u = dq.front(); dq.pop_front();
		rep(v, 1, n + 1) {
			if(M[u][v] == 0 || d[v] != INF) continue;
			d[v] = d[u] + 1;
			dq.eb(v);
		}
	}
	rep(i, 1, n + 1) {
		cout << i << " " << ((d[i] == INF) ? (-1) : d[i]) << endl;
	}
}
int main() {
	cin >> n;
	int u, k, v;
	rep(i, 1, n + 1) {
		cin >> u >> k;
		rep(j, 1, k + 1) {
			cin >> v;
			M[u][v] = 1;
		}
	}
	bfs();
	rep(i, 1, n + 1) {
		rep(j, 1, n + 1) {
			cout << M[i][j] << (j == n ? "\n" : " ");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
queue<int> q;
vector<vector<int> > v(101);
int dp[101];
int n, t = 0;
int main() {
	cin >> n;
	for (int i = 0; i < n;i++) {
		int u, k; cin >> u >> k;
		for (int j = 0; j < k;j++) {
			int x; cin >> x;
			v[u].push_back(x);
		}
	}
	memset(dp, -1, sizeof(dp));
	q.push(1); dp[1] = 0;
	while (!q.empty()) {
		int a = q.front(); q.pop();
		for (int i = 0; i < v[a].size();i++) {
			if (dp[v[a][i]] == -1) {
				dp[v[a][i]] = dp[a] + 1;
				q.push(v[a][i]);
			}
		}
	}
	for (int i = 1; i <= n;i++) {
		cout << i << " " << dp[i] << endl;
	}
}

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

void visit(int kyori[], vector<list<int> > adj){
    int k;
    queue<int> que;
    list<int>::iterator t;
    que.push(1);
    while(!que.empty()){
        k = que.front();
        que.pop();
        for(t = adj[k].begin(); t != adj[k].end(); ++t){
            if(kyori[*t] == -1){
                kyori[*t] = kyori[k] + 1;
                que.push(*t);
            }
        }
    }

}

int main(){
    //input
    int n;
    cin >> n;
    int u, k, v;
    int i, j;
    list<int> list_sub;
    vector<list<int> > adj;
    for(i = 0; i <= n; i++){
        adj.push_back(list_sub);
    }
    for(i = 1; i <= n; i++){
        cin >> u >> k;
        for(j = 1; j <= k; j++){
            cin >> v;
            adj[i].push_back(v);
        }
    }

    //calc
    int kyori[n+1];
    for(i = 2; i <= n; i++){
        kyori[i] = -1;
    }
    kyori[1] = 0;
    visit(kyori, adj);

    //answer
    for(i = 1; i <= n; i++){
        cout << i << " " << kyori[i] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <string.h>

using namespace std;

int main(int argc, char* argv[]) {
  int n;
  cin >> n;

  vector<vector<int> > V(n);
  for (int i = 0; i < n; ++i) {
    int u, k;
    cin >> u >> k;
    V[u - 1].resize(k);
    for (int j = 0; j < k; ++j)
      cin >> V[u - 1][j];
  }

  int d[n];
  memset(d, -1, sizeof(int) * n);
  d[0] = 0;

  queue<int> q;
  q.push(0);

  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int i = 0; i < V[cur].size(); ++i) {
      if (d[V[cur][i] - 1] == -1) {
        q.push(V[cur][i] - 1);
        d[V[cur][i] - 1] = d[cur] + 1;
      }
    }
  }

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

  return 0;
}

C++: #include <bits/stdc++.h>
#include <iomanip>
#include <math.h>
#include <unistd.h>
#include <stdio.h>
#define rep(i,x) for(ll i = 0;i<x;i++)
#define all(a) (a).begin(),(a).end()
#define vecin(a) rep(i,a.size())cin >> a[i]
#define vecout(a) rep(i,a.size())cout << a[i] << " ";cout << "\n"
#define vecvec(a) vector<vector<(a)>>
using ll = long long;
using ld = long double;
using namespace std;
using dou = double;
const ll inf = 2147483647;
const ll INF = 1LL << 60;
const ld pi = 3.14159265358;
const ll mod = 1000000007;
typedef pair<ll,ll> P;
using graph = vector<vector<ll>>;
template<class T> inline bool chmax(T& a, T b){if(a < b){a = b; return true;}return false;}
template<class T> inline bool chmin(T& a, T b){if(a > b){a = b; return true;}return false;}
template<class T> inline bool change(T a,T b){if(a > b){swap(a,b);return true;}return false;}
ll gcd(ll a,ll b){
    if(a < b)swap(a,b);
    if(a % b == 0)return b;
    else return gcd(b,a%b);
}
//素数判定O(sqrt(N))
bool isp(ll n){
    bool res = true;
    if(n == 1)return false;
    else{
        for(ll i = 2;i * i <= n;i++){
            if(n % i == 0){
                res = false;
                break;
            }
        }
        return res;
    }
}
const ll cmax = 1000000;
ll fac[cmax], finv[cmax], inv[cmax];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (ll i = 2; i < cmax; i++){
        fac[i] = fac[i - 1] * i % mod;
        inv[i] = mod - inv[mod%i] * (mod / i) % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
}

// 二項係数計算
ll nCk(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
//nのm乗をMODで割ったあまりO(logN)
ll modpow(ll n,ll m,ll MOD){
    ll res = 1;
    while(m){
        if(m & 1)res = (res * n) % MOD;
        m >>= 1;
        n *= n;
        n %= MOD;
    }
    return res;
}

//printf("%.10f\n", N);
/*  vector<ll> vec = {1,2,5,6,8,9,10};
    cout << binary_search(all(vec),5) << endl; -> true*/
/*
    vector<ll> vec = {1,2,5,6,8,9,10};
    auto n = upper_bound(all(vec),4);
    cout << *n << endl; -> 5*/

//    cout << fixed << setprecision(15);

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll N;
    cin >> N;
    graph G(N);
    ll ten,kazu,to;
    rep(i,N){
        cin >> ten;ten--;
        cin >> kazu;
        rep(j,kazu){
            cin >> to;to--;
            G[ten].push_back(to);
        }
    }
    queue<ll> que;
    vector<ll> dist(N,INF);
    dist[0] = 0;
    que.push(0);
    while(que.size()){
        ll n = que.front();que.pop();
        for(auto x : G[n]){
            if(dist[x] == INF){
                que.push(x);
                dist[x] = dist[n] + 1;
            }
        }
    }
    rep(i,N){
        if(dist[i] == INF){
            cout << i + 1 << " " << -1 << endl;
        }
        else cout << i + 1 << " " << dist[i] << endl;
    }
}