Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", vertices[i].id, vertices[i].visitedAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: /*
 ********************************************************************************
 *  Author: ThoseBygones
 *  Version: V1.0
 *  Date: 2018-06-27
 *  Subject: ACM-ICPC
 *  Language: C/C++11
 *  OJ: AOJ
 *  Algorithm:
 ********************************************************************************
 *  Algo-Description:
 ********************************************************************************
 */

//#include <bits/stdc++.h>  //For CodeForces only

//#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <ctime>
#include <numeric>
#include <vector>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <complex>
#include <deque>
#include <functional>
#include <list>
#include <map>
#include <string>
#include <sstream>
#include <set>
#include <stack>
#include <queue>
using namespace std;
template<class T> inline T sqr(T x)
{
    return x * x;
}
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
typedef pair<LL, LL> PLL;
typedef pair<LL, int> PLI;
typedef pair<LD, LD> PDD;
#define MP make_pair
#define PB push_back
#define sz(x) ((int)(x).size())
#define clr(ar,val) memset(ar, val, sizeof(ar))
#define istr stringstream
#define FOR(i,n) for(int i=0;i<(n);++i)
#define forIt(mp,it) for(__typeof(mp.begin()) it = mp.begin();it!=mp.end();it++)
const double EPS = 1e-6;
const int INF = 0x3fffffff;
const LL LINF = INF * 1ll * INF;
const double PI = acos(-1.0);

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lowbit(u) (u&(-u))
#define MAXN 105

int g[MAXN][MAXN];
bool vis[MAXN];
int step[MAXN];
int n;

void bfs(int x)
{
    vis[x]=true;
    queue <int> q;
    step[x]=0;
    q.push(x);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int v=1; v<=n; v++)
        {
            if(g[u][v] && !vis[v])
            {
                vis[v]=true;
                step[v]=step[u]+1;
                q.push(v);
            }
        }
    }
}

int main()
{
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        int u,num;
        scanf("%d%d",&u,&num);
        while(num--)
        {
            int v;
            scanf("%d",&v);
            g[u][v]=1;
        }
    }
    bfs(1);
    for(int i=1; i<=n; i++)
    {
        if(!vis[i])
            step[i]=-1;
    }
    for(int i=1; i<=n; i++)
        printf("%d %d\n",i,step[i]);
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef float fl;
typedef long long ll;

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

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

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

int main(){
    cin >> n;
    int u,k,v;
    rep(i,n) rep(j,n) M[i][j] = 0;
    rep(i,n){
        cin >> u >> k;
        rep(j,k){
            cin >> v;
            M[u-1][v-1] = 1;
        }
    }
    bfs(0);
    return 0;
}

C++: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF*INF*10;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vvi G(n);
    rep(i, n) {
        int id, k;
        cin >> id >> k;
        id--;
        G[id].resize(k);
        rep(j, k) {
            cin >> G[id][j];
            G[id][j]--;
        }
    }

    vi dist(n, -1);
    dist[0] = 0;
    queue<int> q;
    q.push(0);
    while(!q.empty()) {
        int now = q.front();
        q.pop();
        for(int ne:G[now]) {
            if(dist[ne] != -1) continue;
            dist[ne] = dist[now] + 1;
            q.push(ne);
        }
    }

    rep(i, n) {
        cout << i + 1 << ' ' << dist[i] << endl;
    }
}