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 <iostream>
#include <string>
#include <vector>
#include <queue>

using namespace std;

template<typename ISTREAM>
void readGraphList(ISTREAM& cin, vector< vector<int> >& graphList) {
	size_t n;
	cin >> n;
	graphList = vector< vector<int> >(n);
	for (size_t i = 0; i != n; ++i) {
		size_t u;
		size_t k;
		cin >> u;
		cin >> k;
		vector<int>& v = graphList.at(u - 1);
		v.resize(k);
		for (size_t j = 0; j != k; ++j) {
			cin >> v.at(j);
			v.at(j)--;
		}
	}
}
#if 1
void widthSearch(const vector<vector<int>>& graphList) {
	const size_t n = graphList.size();
	queue<int> nodeQueue;
	vector<int> distanceList(n, -1);
	distanceList.front() = 0;
	nodeQueue.push(0);
	while (!nodeQueue.empty()) {
		const int id = nodeQueue.front();
		const int distanceOfNextNode = distanceList.at(id) + 1;
		const vector<int>& adjList = graphList.at(id);
		for (const auto& adjID : adjList) {
			int& nextNodeDistance = distanceList.at(adjID);
			if (nextNodeDistance != -1)continue;
			nodeQueue.push(adjID);
			nextNodeDistance = distanceOfNextNode;
		}
		nodeQueue.pop();
	}
	for (int i = 0; i != n; ++i) {
		cout << i + 1 << " " << distanceList.at(i) << endl;
	}
}
#endif

template<typename T>
void execute(T& cin) {
	vector< vector<int> > graphList;
	readGraphList(cin, graphList);
	widthSearch(graphList);
}

int main()
{
	execute(cin);

	return 0;
}

C++: #include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
/*
using ll = long long;
using P = pair<int, int>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
*/

int to[105][105];

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<vector<int> > G(n);
    rep(i,n){
        int u, k;
        cin >> u >> k;
        u--;
        rep(j,k){
            int v;
            cin >> v;
            v--;
            to[u][v] = 1;
        }
    }
    vector<int> dist(n, -1);
    queue<int> que;
    dist[0] = 0;
    que.push(0);
    while(!que.empty()){
        int v = que.front();
        que.pop();
        rep(nv,n){
            if(!to[v][nv]) continue;
            if(dist[nv] != -1) continue;
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }
    rep(i,n) cout << i+1 << " " << dist[i] << endl;
    return 0;
}

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

using namespace std;
#define MAX 100

const int INF = (1<<21);
int mat[MAX][MAX] = {};
int n, d[MAX] = {};
bool c[MAX];

void bfs(int u){
    queue<int> p; 
    int now = 0;
    d[u] = now++;
    c[u] = true;

    p.push(u);
    while(!p.empty()){
        int x = p.front(); p.pop();
        for(int i = 0; i < n; i++){
            if(mat[x][i] && !c[i]){
                p.push(i);
                c[i] = true;
                d[i] = d[x]+1;
            }
        }
    }
}

int main(){
    cin >> n;
    
    for(int i = 0; i < n; i++){
        int x, k, y;
        cin >> x >> k;
        while(k-- > 0){
            cin >> y;
            mat[x-1][y-1] = 1;
        }
    }

    for(int i = 0; i < n; i++) d[i] = INF;
    
    bfs(0);

    for(int i = 0; i < n; i++){
        cout << (i+1) << " " << (d[i] == INF ? -1 : d[i]) << endl;
    }
    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[u]!=0){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,v,k;
    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 <iostream>
#include <cstdio>
#define MAX 101
using namespace std;

int n;
int a[MAX][MAX]={};
int d[MAX];

void track(int v,int depth){
  if(d[v]>depth||d[v]==-1){
    d[v]=depth;
  }else if(d[v]<depth){
    return;
  }
  for(int i=1;i<=n;i++){
    if(a[v][i]==1){
      track(i,depth+1);
    }
  }
}

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

  for(int i=1;i<=n;i++){
    d[i]=-1;
  }

  track(1,0);

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

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

int n, d[MAX];
vector<vector<int> > vec;
vector<int> ve;
queue<int> Q;

void bfs(int s) {
	for (int i = 1; i <= n; i++) {
		d[i] = -1;
	}
	d[s] = 0;
	Q.push(s);
	int now, cnt;
	while (!Q.empty()) {
		now = Q.front();
		Q.pop();
		for (cnt = 1; cnt <= vec[now][0]; cnt++) {
			if (d[vec[now][cnt]] == -1) {
				Q.push(vec[now][cnt]);
				d[vec[now][cnt]] = d[now] + 1;
			}
		}
	}
}

int main(void) {
	vec.push_back(ve);
	int u, k, v, i;
	cin >> n;
	for (i = 0; i < n; i++) {
		ve.clear();
		cin >> u >> k;
		ve.push_back(k);
		while (k--) {
			cin >> v;
			ve.push_back(v);
		}
		vec.push_back(ve);
	}

	bfs(1);

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