Go: package main

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

type AdjacentList struct {
	matrix [][]int
}

func NewEmptyAdjacentList(dim int) *AdjacentList {
	matrix := make([][]int, dim)
	for i := 0; i < dim; i++ {
		matrix[i] = make([]int, dim)
	}
	return &AdjacentList{matrix: matrix}
}

func (al *AdjacentList) set(node int, adjacent int) {
	al.matrix[node-1][adjacent-1] = 1
}

func (al *AdjacentList) findAdjacentsFor(node int) []int {
	adjacents := []int{}
	for i, v := range al.matrix[node-1] {
		if v == 1 {
			adjacents = append(adjacents, i+1)
		}
	}
	return adjacents
}

type DistanceList struct {
	list []int
}

func NewDistanceList(dim int) *DistanceList {
	list := make([]int, dim)
	for i := 0; i < dim; i++ {
		list[i] = -1
	}
	return &DistanceList{list: list}
}

func (dl *DistanceList) set(node int, distance int) {
	dl.list[node-1] = distance
}

func (dl *DistanceList) get(node int) int {
	return dl.list[node-1]
}

func (dl *DistanceList) isVisited(node int) bool {
	return dl.list[node-1] >= 0
}

type Position struct {
	node     int
	distance int
}

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

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

	al := NewEmptyAdjacentList(dim)

	for i := 1; i <= dim; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}

		adjacentNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		for j := 0; j < adjacentNum; j++ {
			adjacent, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			al.set(node, adjacent)
		}
	}

	dl := NewDistanceList(dim)

	bfs(al, dl)

	for i := 1; i <= dim; i++ {
		fmt.Printf("%d %d\n", i, dl.get(i))
	}
}

func bfs(al *AdjacentList, dl *DistanceList) {
	stack := []Position{Position{node: 1, distance: 0}}
	for len(stack) > 0 {
		currentPosition := stack[0]
		stack = stack[1:]
		if !dl.isVisited(currentPosition.node) {
			dl.set(currentPosition.node, currentPosition.distance)
		}
		for _, v := range al.findAdjacentsFor(currentPosition.node) {
			if !dl.isVisited(v) {
				stack = append(stack, Position{node: v, distance: currentPosition.distance + 1})
			}
		}
	}
}

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

C++: #include "bits/stdc++.h"

using namespace std;

using ll = long long;

template<typename T> using V = std::vector<T>;
using Vi = V<int>;
using VVi = V<V<int>>;
using Vl = V<ll>;
using VVl = V<V<ll>>;
using Vs = V<string>;

template<typename T1, typename T2> using P = std::pair<T1, T2>;
using Pii = P<int, int>;
using Pll = P<ll, ll>;
using Pdd = P<double, double>;

template<typename T1, typename T2> using M = std::map<T1, T2>;
using Mii = M<int, int>;
using Msi = M<string, int>;

#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define REP2(i,s,e) for(int i=(int)(s);i<(int)(e);++i)
#define RREP(i,s,e) for(int i=(int)(s);i>=(int)(e);--i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin(),ie=(c).end();i!=ie;++i)
#define ALL(c) (c).begin(), (c).end()

const double PI = acos(-1);
const ll MOD = 1e9+7;

int n;
string s;

int main() {
    cin >> n;
    VVi edges(n);
    REP(i, n) {
        int u, k;
        cin >> u >> k;
        edges[i] = Vi(k);
        REP(j, k) {
            int v;
            cin >> v;
            v--;
            edges[i][j] = v;
        }
    }

    Vi dis(n, -1);
    queue<int> q;
    q.push(0);
    dis[0] = 0;
    while (!q.empty()) {
        int node = q.front();
        q.pop();
        FOR(i, edges[node]) {
            if (dis[*i] == -1) {
                dis[*i] = dis[node] + 1;
                q.push(*i);
            }
        }
    }
    REP(i, n) {
        printf("%d %d\n", i+1, dis[i]);
    }

    return 0;
}

C++: //
//  main.cpp
//  BFS
//  Copyright ?????? 2017 1. All rights reserved.
//

#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;


int main() {
    int n,u,k,d;
    vector<vector<int>>A(100,vector<int>(100));
    for(int i=0;i<100;i++)
        for(int j=0;j<100;j++)
            A[i][j]=0;
    
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>u>>k;
        for(int j=0;j<k;j++){
            cin>>d;
            A[u-1][d-1]=1;
        }
    }//input
    
    int D[100];
    for(int i=0;i<100;i++) D[i]=100000000;
    int src=0;
    queue<int>Q;
    Q.push(src);
    D[src]=0;
        while(!Q.empty()){
            int cur=Q.front();
            Q.pop();
            for(int dst=0;dst<n;dst++)
            {
                if(A[cur][dst]==1&&D[dst]==100000000){
                    
                    D[dst]=min(D[cur]+1,D[dst]);
                    Q.push(dst);
                }
        }
    }
    
    for(int i=0;i<n;i++){
        
        cout<<i+1<<" "<<D[i]<<endl;
    }
    
    

    
    
    }

C++: #include<iostream>
#include<queue>
using namespace std;
void debug1();
int n, gragh[110][110] = {}, d[110] = {};
queue<int> q;

int main() {
	cin >> n;
	//input
	for (int i = 0; i < n; i++) {
		int u,k;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			int t;
			cin >> t;
			gragh[u][t] = 1;
		}
	}
	//debug1();
	//search
	d[1] = 0;
	q.push(1);
	while (!q.empty()) {
		int cur = q.front();
		q.pop();
		//cerr << "visiting " << cur << " Distance: " << d[cur] << endl;
		for (int i = 1; i <= n; i++) {
			if (d[i] == 0 && gragh[cur][i] == 1) {
				d[i] = d[cur] + 1;
				q.push(i);
			}
		}
	}

	//from 0 to 1
	for (int i = 2; i <= n; i++) {
		if (d[i] == 0) d[i] = -1;
	}

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

void debug1() {
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cerr << gragh[i][j] << " " ;
		}
		cerr << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <cstdlib>
#include <queue>
#include <vector>
#include <set>

using namespace std;
int n,m,l,p;
bool a[100][100];
int D[100];
void  bfs ( int src) {
	queue<int> Q;
	Q.push(src);
	D[src]=0;
	while( ! Q.empty() ) {
		int cur = Q.front();
		Q.pop();
		for(int dst = 0; dst < n; dst++ ) {
			if( D[dst] == 0 && a[cur][dst] == 1 ) {
				D[dst] = D[cur] + 1;
				Q.push(dst);
			}
		}
	}
	for (int i = 0; i < n;i++) {
	cerr << i+1 <<  " " << D[i] << endl;
	}
}
int main() {
	cin >> n;
	for (int i = 0;i < n; i++){
		cin >> m;
		cin >> l;
		for(int j = 0; j < l;j++) {
			a[i][j] = 0;
			cin >> p;
			a[m-1][p-1] = 1;
		}
	}
		bfs(0);
	
}

C++: #include "bits/stdc++.h"
#include <unordered_set>

#define _CRT_SECURE_NO_WARNINGS

#define REP(i, n) for(decltype(n) i = 0; i < (n); i++)
#define REP2(i, x, n) for(decltype(x) i = (x); i < (n); i++)
#define REP3(i, x, n) for(decltype(x) i = (x); i <= (n); i++)
#define RREP(i, n) for (decltype(n) i = (n) - 1; i >= 0; i--)

#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

using namespace std;

const int INF = 1000000000;
const double PI = 3.1415926535897932384626433832795;

// N, E, S, W
const int dx[4] = { -1, 0,  1,  0 };
const int dy[4] = { 0, 1,  0, -1 };

using LL = long long int;
using LD = long double;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pll = pair<LL, LL>;
using vi = vector<int>;
using vd = vector<double>;
using vs = vector<string>;
using vvi = vector<vi>;
using vll = vector<LL>;

//==============================================
int n;
vi D;
vvi M;

void print() {
	REP3(i, 1, n) {
		cout << i << " " << ((D[i] == INF) ? -1 : D[i]) << "\n";
	}
}

void antBfs() {
	D[1] = 0;
	queue<int> q;
	q.push(1);

	int u;
	while (!q.empty()) {
		u = q.front(); q.pop();
		REP3(i, 1, n) {
			if (M[u][i] == 0) continue;
			if (D[i] != INF) continue;
			D[i] = D[u] + 1;
			q.push(i);
		}
	}

	print();
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> n;

	D.resize(n + 1, INF);
	M.resize(n + 1, vi(n + 1));

	int u, k, v;
	REP3(i, 1, n) {
		cin >> u >> k;
		REP3(j, 1, k) {
			cin >> v;
			M[u][v] = 1;
		}
	}

	antBfs();
	return 0;
}