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++: // AOJ
// ALDS1_11_C Breadth First Search
// https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_11_C
#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
#define rep(i, n) for(int i = 0; i < (n); i++)

int main()
{
    int n; cin >> n;
    vvi g(n, vi(n, 0));
    rep(i, n) {
        int u, k; cin >> u >> k; u--;
        rep(j, k) {
            int v; cin >> v; v--;
            g[u][v] = 1;
        }
    }

    vi ans(n, -1);
    queue<int> q;
    q.push(0);
    ans[0] = 0;
    while (!q.empty()) {
        int u = q.front(); q.pop();
        rep(i, n) {
            if (g[u][i] == 1 && ans[i] == -1) {
                q.push(i);
                ans[i] = ans[u]+1;
            }
        }
    }

    rep(i, n) cout << i+1 << " " << ans[i] << endl;
}

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

using namespeace std;
static const int N = 100;
static const int INFTY = (1<<21);

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

void dfs(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[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 u, k, v;


  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 <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[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 u, v, k;
    cin >> n;

    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            M[i][i] = 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);
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace  std;

struct StDir
{
  int m_nBgn;
  int m_nEnd;

  StDir(int nBgn, int nEnd) : m_nBgn(nBgn), m_nEnd(nEnd)
  { }
};

void fnInput(queue<StDir>& rqoBFSrch, vector<int>& rvnDst)
{
  int nxSiz;
  cin >> nxSiz;

  rvnDst.resize(nxSiz + 1, 0);
  for (int i = 0; i < nxSiz; ++i)
  {
    int nx, nySiz;
    cin >> nx >> nySiz;
    for (int j = 0; j < nySiz; ++j)
    {
      int ny;
      cin >> ny;
      StDir oDir(nx, ny);
      rqoBFSrch.push(oDir);
    }
  }
}

void fnBreadthFirstSearch(queue<StDir>& rqoBFSrch, vector<int>& rvnDst)
{
  while (rqoBFSrch.size())
  {
    StDir oDir = rqoBFSrch.front(); rqoBFSrch.pop();

    int nDst = rvnDst[oDir.m_nBgn] + 1;
    if (!rvnDst[oDir.m_nEnd])
      rvnDst[oDir.m_nEnd] = nDst;
    else
      rvnDst[oDir.m_nEnd] = min(rvnDst[oDir.m_nBgn], nDst);
  }
}

void fnResult(const vector<int>& cnrvnDst)
{
  for (int i = 1; i < cnrvnDst.size(); ++i)
    cout << i <<  " " << cnrvnDst[i] << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  queue<StDir> qoBFSrch;
  vector<int>  vnDst;

  fnInput(qoBFSrch, vnDst);
  fnBreadthFirstSearch(qoBFSrch, vnDst);
  fnResult(vnDst);

  return 0;
}

C++: #include<iostream>
using namespace std;

class node {
public:
	int d = -1, k, v[100];
	bool t = false;
};

node V[101];
int E[101], e1 = 0, e2 = 0;

void Serch(int u) {
	for (int i = 0; i < V[u].k; i++) {
		if (V[V[u].v[i]].t == false) {
			E[e1] = V[u].v[i]; e1++;
			V[V[u].v[i]].t = true;
			V[V[u].v[i]].d = V[u].d+1;
		}
	}
	if (e2 < e1) { e2++; Serch(E[e2]); }
}

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

	for (int i = 1; i <= n; i++) {
		E[0] = 1; V[1].t = true; V[1].d = 0; e1++;
		Serch(E[e2]);
	}

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

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

using namespace std;

int D[110]={0};
void bfs(int src,int n,int g[110][110]){

queue<int> Q;
Q.push(src);
D[src]=0;
while (! Q.empty()){
	int cur =Q.front();
	Q.pop();
	for(int r=0;r<n;r++){
		if (g[cur][r]==1&&D[r]==0){
			D[r]=D[cur]+1;
			Q.push(r);
		}
	}
}
	
}



int main() {
int n,g[110][110]={0};
cin>>n;
int j,num,t;
for(int k=0;k<n;k++){
cin>>j>>num;
for(int kk=0;kk<num;kk++){
	cin>>t;
	g[k][t-1]=1;
}
}
bfs(0,n,g);
cout<<"1 0"<<endl;
for(int a=1;a<n;a++){
	if(D[a]!=0){
	cout<<a+1<<" "<<D[a]<<endl;
}
else{
	cout<<a+1<<" -1"<<endl;
}
}
	return 0;
}

C++: #include<cstdio>
#include<queue>
using namespace std;
static const int INF=(1<<21);
static const int MAXN=101;
struct p{
	int d;
	int cnt;
};
int main()
{
	int n,m[MAXN][MAXN],i,j,u,k,v,l,o,t[MAXN];
	p e,f;
	queue<p> q;
	bool used[MAXN]={};
	scanf("%d",&n);
	for(i=1;i<n+1;i++){
		for(j=1;j<n+1;j++){
			m[i][j]=INF;
		}
	}
	for(i=0;i<n;i++){
		scanf("%d %d",&u,&k);
		for(j=0;j<k;j++){
			scanf("%d",&v);
			m[u][v]=1;
		}
	}
	used[1]=true;
	e.d=1;
	e.cnt=0;
	q.push(e);
	while(!q.empty()){
		e=q.front();
		q.pop();
		o=0;
		for(i=1;i<n+1;i++){
			if(m[e.d][i]==1&&used[i]==false){
				f.d=i;
				f.cnt=e.cnt+1;
				q.push(f);
				used[i]=true;
				t[i]=e.cnt+1;
				o=1;
			}
		}
	}
	for(i=1;i<n+1;i++){
		if(used[i]==true){
			printf("%d %d\n",i,t[i]);
		}
		else{
			printf("%d -1\n",i);
		}
	}
	return 0;
}