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<cstdio>
#include<iostream>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define _USE_MATH_DEFINES // M_PI????????????????????????
#include<math.h>
#include<queue>
#pragma warning(disable:4996)
using namespace std;

static const int N=100;
static const int INFTY = (1<<21);

int n,M[N][N];
int d[N]; // ?????¢??§?¨??????¶??????????????????

void bfs(int s){
	queue<int> q;
	q.push(s);
	for(int i=0; i<n; i++) d[i] = INFTY;
	d[s] = 0;
	int u;
	while( !q.empty() ){
		u = q.front();
		q.pop();
		for(int v=0; v<n; v++){
			if( M[u][v]==0 ) continue;
			if( d[v]!=INFTY ) continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}
	for(int i=0; i<n; i++){
		cout << i+1 << " " << ( (d[i]==INFTY)?(-1):d[i] ) << "\n";
	}
}

int main(){
	int u, k, v;
	cin >> n;

	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++) M[i][j] = 0;
	}
	for(int i=0; i<n; i++){
		cin >> u >> k;
		u--;
		for(int j=0; j<k; j++){
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}
	bfs(0);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,o,n) for(long long i = o;i<n;i++)
#define oneforall ios::sync_with_stdio(false);cin.tie(0);
#define all(v) (v).begin(),(v).end()
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define int long long 
const long long inf=1e18;
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long > > vpll;
typedef vector<pair<int, int > > vpii;
#define FORR(x,arr) for(auto& x:arr)
#define ZERO(a) memset(a,0,sizeof(a))





//3111111111111111111111111111111

	int n;
	vi adj[310];
	bool visit[310];
	int dep[310];
	void bfs(){

		visit[1] = 1;
		queue<int> queue1;
		queue1.push(1);
		while(!queue1.empty()){
			int tmp = queue1.front();
			queue1.pop();
			FORR(x1,adj[tmp])if(!visit[x1]){
				visit[x1] = 1;
				queue1.push(x1);
				dep[x1] = dep[tmp]+1;
			}
		}
	}

	void solve(){
		
		in(n);
		memset(dep,-1,sizeof(dep));
		dep[1] = 0;
		FOR(i,0,n){
			ini(x,x1);
			FOR(i1,0,x1){
				ini(tmp);
				adj[x].push_back(tmp);
			}
		}
		bfs();
		FOR(i,1,n+1){
			out(i,dep[i]);
		}







		
		
	}























int32_t main() {
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall 
	oneforall

	solve();
	


	return 0;
}

C++: #include <iostream>
#include <queue>

using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);

int n, M[N][N];
int d[N];

void bfs(int s)
{
    queue<int> q;
    q.push(s);
    for (int i = 0; i < n; i++)
        d[i] = INFTY;
    d[s] = 0;
    int u;
    while (!q.empty())
    {
        u = q.front();
        q.pop();
        for (int v = 0; v < n; v++)
        {
            if (M[u][v] == 0)
                continue;
            if (d[v] != INFTY)
                continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for (int i = 0; i < n; i++)
        cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
}

int main()
{
    int u, k, v;

    cin >> n;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            M[i][j] = 0;

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

    bfs(0);

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    const ll inf = LONG_LONG_MAX / 2 - 1;
    ll n;
    cin >> n;
    vector<vector<ll>> g(n);
    rep(i, n) {
        ll u, k;
        cin >> u >> k;
        u--;
        rep(j, k) {
            ll v;
            cin >> v;
            v--;
            g[u].push_back(v);
        }
    }
    vector<ll> dist(n, inf);
    queue<ll> q;
    q.push(0);
    dist[0] = 0;
    while(!q.empty()) {
        ll u = q.front(); q.pop();
        for(auto v : g[u]) {
            if (dist[v] <= (dist[u] + 1)) continue;
            dist[v] = dist[u] + 1;
            q.push(v);
        }
    }
    rep(i, n) printf("%lld %lld\n", i + 1, (dist[i] == inf) ? -1 : dist[i]);
    return 0;
}