Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(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);
}