Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 0
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int n,d[100];
bool graph[100][100], visited[100];
queue<int> q;


void bfs(int s){
    q.push(s);

    while(!q.empty()){
        int v=q.front(); q.pop();
        for(int v2=0; v2<n; v2++){
            if(graph[v][v2] && visited[v2]==false){
                d[v2]=d[v]+1; visited[v2]=true;
                q.push(v2);
            }
        }
    }
}


int main(void){
    cin >> n;

    for(int i=0; i<n; i++){
        visited[i]=false;
        d[i]=-1;
        for(int j=0; j<n; j++) graph[i][j]=false;
    }
    for(int i=0; i<n; i++){
        int v,k; cin >> v >> k;
        for(int j=0; j<k; j++){
            int w; cin >> w;
            graph[v-1][w-1]=true;
        }
    }
    d[0]=0;


    q.push(0);
    bfs(0);

    for(int i=0; i<n; i++){
        printf("%d %d\n",i+1, d[i]);
    }

}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;

#define Rep(b, e, i) for(int i = b; i <= e; i++)
#define Repr(e, b, i) for(int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n-1, i)
#define repr(n, i) Repr(n-1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second
#define Pqaz(T) priority_queue<T,vector<T>,greater<T>>
#define Pqza(T) priority_queue<T>
#define ENJYU std::ios::sync_with_stdio(false);std::cin.tie(0);

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;

//vector の中身を出力
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}

const int MAX = 128;

vector <int> adj[MAX];
int d[MAX];

void solve(void){
	int N;
	cin >> N;
	rep(N, i) {
		int u, k;
		cin >> u >> k;
		u--;
		rep(k, i) {
			int v;
			cin >> v;
			v--;
			adj[u].pb(v);
		}
	}
	memset(d, -1, sizeof(d));
	queue <int> q;
	q.push(0);
	d[0] = 0;
	while (!q.empty()) {
		int n = q.front();
		q.pop();
		for (int nv : adj[n]) {
			if (d[nv] > 0) continue;
			d[nv] = d[n] + 1;
			q.push(nv);
		}
	}

	rep(N, i) cout << i+1 << ' ' << d[i] << endl;

}

int main(void){
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const int inf = 1e9;
const ll INF = 1e18;
const double pi = 3.14159265358979323846;
#define rep(i, n) for (int i = 0; i < n; i++)
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };

int main() {
    int n;
    cin >> n;
    Graph g (n);
    queue<int> q;
    rep (i, n) {
        int a, k;
        cin >> a >> k;
        rep (j, k) {
            int b;
            cin >> b;
            g[a - 1].push_back (b - 1);
        }
    }
    int dist[n] = {};
    rep (i, n) dist[i] = -1;
    dist[0] = 0;
    q.push (0);
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        for (auto p : g[now]) {
            if (dist[p] != -1) continue;
            dist[p] = dist[now] + 1;
            q.push (p);
        }
    }
    rep (i, n) {
        cout << i + 1 << " " << dist[i] << "\n";
    }
}

C++: //
// Created by tattsun on 2017/02/14.
//

#include <iostream>
#include <vector>

#define VERTEX_NUM_MAX 101

using namespace std;
typedef vector<int> Vertex;

int n;
Vertex vs[VERTEX_NUM_MAX];
int ds[VERTEX_NUM_MAX];

void _bfs(vector<int>& targets, int distance) {
    vector<int> next_targets;

    for(auto it = targets.begin(); it != targets.end(); it++) {
        if(ds[*it] == -1) {
            ds[*it] = distance;

            auto& t = vs[*it];
            for(auto itt = t.begin(); itt != t.end(); itt++)
                next_targets.push_back(*itt);
        }
    }

    if(next_targets.empty())
        return;

    _bfs(next_targets, distance+1);
}

void bfs() {
    vector<int> targets;
    targets.push_back(1);

    _bfs(targets, 0);
}

int main() {
    for(int i=0; i<VERTEX_NUM_MAX; i++) {
        ds[i] = -1;
    }

    cin >> n;

    int u, k, p;
    for(int i=1; i<=n; i++) {
        cin >> u >> k;
        for(int j=0; j<k; j++) {
            cin >> p;
            vs[i].push_back(p);
        }
    }

    bfs();

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

C++: #include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (a); i >= (int)(b); i--)
#define all(x) (x).begin(), (x).end()
using i32 = int32_t;
using i64 = int64_t;
using f32 = float;
using f64 = double;
using P   = pair<int, int>;

template <class T>
bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    } else {
        return false;
    }
}
template <class T>
bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
void dump_vec(const vector<T>& v) {
    auto len = v.size();
    rep(i, 0, len) {
        cout << v[i] << (i == (int)len - 1 ? "\n" : " ");
    }
}

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} FASTIO;

//---------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------

signed main() {
    int N;
    cin >> N;
    vector<vector<int>> G(N);
    rep(i, 0, N) {
        int id, k;
        cin >> id >> k;
        id--;
        rep(j, 0, k) {
            int u;
            cin >> u;
            u--;
            G[id].push_back(u);
        }
    }

    const auto inf = 1 << 30;
    vector<int> dist(N, inf);
    dist[0] = 0;
    queue<P> q;
    q.push(make_pair(0, 0));
    while (!q.empty()) {
        auto pi = q.front();
        q.pop();
        auto u = pi.first, cost = pi.second;
        for (auto& to : G[u]) {
            if (chmin(dist[to], cost + 1)) {
                q.push(make_pair(to, dist[to]));
            }
        }
    }

    rep(i, 0, N) {
        cout << i + 1 << " " << (dist[i] == inf ? -1 : dist[i]) << "\n";
    }
}