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;

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

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

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

int main(void) {
    int u,v,k;
    cin>>n;
    for(int i=0;i<n;i++) {
        for(int j=0;j<n;j++) g[i][j]=0;
    }
    for(int i=0;i<n;i++) {
            cin>>u>>k;
            u--;
        for(int j=0;j<k;j++) {
            int v;cin>>v;
            v--;
            g[u][v]=1;
        }
    }
    bfs(0);
    return 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);
	nodeQueue.push(0);
	while (!nodeQueue.empty()) {
		const int id = nodeQueue.front();
		int& currentNodeDistance = distanceList.at(id);
		++currentNodeDistance;
		const vector<int>& adjList = graphList.at(id);
		for (const auto& adjID : adjList) {
			int& adjDistance = distanceList.at(adjID);
			if (adjDistance != -1)continue;
			nodeQueue.push(adjID);
			adjDistance = currentNodeDistance;
		}
		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 <iostream>
#include <queue>
using namespace std;
#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define NIL -1
int color[MAX],d[MAX];
int N[MAX][MAX];
queue<int> Q;
int n;
void bfs(int u){
    Q.push(u);
    color[u] = GRAY;
    d[u] = 0;
    while(!Q.empty()){
        u = Q.front();
        Q.pop();
        for(int v=0;v<n;v++){
            if(N[u][v]!=0&&color[v]==WHITE){
                Q.push(v);
                d[v] = d[u]+1;
                color[v] = GRAY;
            }
        }
        color[u] = BLACK;
    }
}
void bfs_init(){
    for(int i=0;i<n;i++){
        d[i] = NIL;
    }
    for(int i=0;i<n;i++){
        color[i] = WHITE;
    }
    bfs(0);
    for(int i=0;i<n;i++){
        cout<<i+1<<" "<<d[i]<<endl;
    }
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            N[i][j] = 0;
        }
    }
    for(int i=0;i<n;i++){
        int u,k;
        cin>>u>>k;
        for(int j=0;j<k;j++){
            int v;
            cin>>v;
            N[i][v-1] = 1;
        }
    }
    bfs_init();
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
 
queue<pair<int,int> > qu;
vector<int> v[111];
int root;
int b;
int d[111];
pair<int, int> pa;
int g;
 
void wide(int num){
 
  memset(d,-1,sizeof(d));
  d[num] = 0;
  qu.push(make_pair(num,d[1]));
 
  while(!qu.empty()){
 
    pa = qu.front();
    qu.pop();
    int id = pa.first;
 
    for(int i = 0; i < (int)v[id].size(); i++){
      if(d[v[id][i]] == -1){
    d[v[id][i]] = pa.second + 1;
    qu.push(make_pair(v[id][i],d[v[id][i]]));
      }
    }
  }
  for(int i = 1; i <= g; i++){
    cout << i << " " << d[i] << endl;
  }
 
}
 
int main(){
 
  
  cin >> g;
  int n;
  for(int i = 0; i < g; i++){
    cin >> n >> b;
    for(int j = 0; j < b; j++){
      cin >> root;
      v[n].push_back(root);
    }
  }
 
  wide(1);
 
}

C++: #include <iostream>
#include <vector>
#include <queue>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
using Graph = vector<vector<int>>;

int main(){
  int n;
  cin >> n;
  Graph G(n);
  rep(i, n){
    int u, k;
    cin >> u >> k;
    u--;
    rep(j, k){
      int v;
      cin >> v;
      v--;
      G[u].push_back(v);
    }
  }
  
  vector<int> d(n, -1);
  queue<int> que;
  
  d[0] = 0;
  que.push(0);
  
  while(!que.empty()){
    int v = que.front();
    que.pop();
    for(int nv : G[v]){
      if (d[nv] != -1) continue;
      
      d[nv] = d[v] + 1;
      que.push(nv);
    }
  }
  
  rep(i, n) cout << i + 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], 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()) {
		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 v, k ,u;
	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;
		for(int j = 0; j < k; j++) {
			cin >> v;
			M[u-1][v-1] = 1;
		}
	}
	
	bfs(0);
	
	return 0;
	
}

C++: #include<bits/stdc++.h>
using namespace std;
int n;
int a[101][101];
int d[101];

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

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