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

using namespace std;


int main(){
    int n;
    cin >> n;
    vector<vector<int> > adj(110);
    int D[110];
    for(int i = 0; i < n; i++){
        int u, k;
        cin >> u >> k; 
        D[u] = -1;
        for(int j = 0; j < k; j++){
            int v;
            cin >> v;
            adj[u].push_back(v);
        }
    }
    queue<int> Q;
    Q.push(1);
    D[1] = 0;
    while(!Q.empty()){
        int cur = Q.front(); 
        Q.pop();
        for(int i = 0; i < adj[cur].size(); i++){
            int dst = adj[cur][i];
            if(D[dst] == -1){
                D[dst] = D[cur]+1;
                Q.push(dst);
            }
        }
    }
    for (int i = 1; i < n+1; i++){
        cout << i << " " << D[i] << endl;
    }
}

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

using namespace std;
const static int N=100;
const static 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;
    while(!q.empty()){
        int 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]<<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; k++){
            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);

#define rep(i,n)for(int i=0;i<n;i++)

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

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

int main()
{
    int u,k,v;

    cin>>n;
    rep(i,n)
    {
        rep(j,n)M[i][j]=0;
    }
    
    rep(i,n)
    {
        cin>>u>>k;
        u--;
        rep(j,k)
        {
            cin>>v;
            v--;
            M[u][v]=1;
        }
    }
    bfs(0);

    return 0;
}

C++: #include <bits/stdc++.h>
 
#define rep(i,n) for(ll i=0;i<n;++i)
#define ALL(a) (a).begin(),(a).end()
 
using namespace std;
using ll  = long long;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
 
const ll MOD  = 1e9+7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;

int main(){
    int n;
    cin >> n;

    vector<vector<ll>> g(n);

    rep(i, n){
        int u, k;
        cin >> u >> k;

        rep(j, k){
            int v;
            cin >> v;
            v--;
            g[i].push_back(v);
        }
    }

    queue<int> que;
    que.push(0);

    ll dist[n];
    rep(i, n)dist[i] = -1;
    dist[0] = 0;

    bool visited[n] = {};
    visited[0] = true;

    while(!que.empty()){
        int pos = que.front();

        que.pop();

        rep(i, g[pos].size()){
            int child = g[pos][i];

            if(visited[child])continue;
            visited[child] = true;

            dist[child] = dist[pos] + 1;

            que.push(child);
        }
    }

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

C++: #include<iostream>
#include<cstdio>
#define INF 1<<30
#define MAX 100000
using namespace std;
  
class Queue{
private:
  int inf[MAX];
  int head,tail;
   
public:
   
  Queue(){
    syokika();
  }
   
  void syokika(){
    head = 0;
    tail = 0;
  }
   
  void push(int x){
    inf[tail]=x;
    if(MAX == tail+1)tail = 0;
    else tail++;
  }
   
  void pop(){
    if(MAX == head+1)head=0;
    else head++;
  }
   
  int front(){
    return inf[head];
  }
   
  bool empty(){
    if(tail - head == 0)return true;
    else return false;
  }
   
  int size(){
    return tail - head;
  }
   
};
  
int imap[200][200];
int k[200] = {0};
int ans[200] = {0};
bool used[200];
  
int main(){
  int n,u[1000];
  cin >> n;
  for(int i=0;i<200;i++)for(int j=0;j<200;j++)imap[j][i] = INF;
  
  for(int i=0;i<200;i++){
    k[i]=ans[i]=u[i]=-1;
  }
  
  for(int i=0;i<n;i++){
    cin >> u[i];
    cin >> k[u[i]];
    for(int j=0;j<k[u[i]];j++){
      cin >> imap[u[i]][j];
    }
  }
  
   int h;
  for(int i=0;i<n;i++){
    int Min = i;
    for(int j=i;j<n;j++){
      if(u[Min] > u[j])Min = j;
    }
    h = u[Min];
    u[Min] = u[i];
    u[i] = h;
  }
  
  Queue que = Queue();
  
  que.push(u[0]);
  ans[u[0]]=0;
  
  while(!que.empty()){
    int p = que.front(); que.pop();
    for(int i=0;i<k[p];i++){
      if(!used[imap[p][i]]){
    que.push(imap[p][i]);//cout << "push!= " << imap[p][i] << endl;
    used[imap[p][i]] = true;
    ans[imap[p][i]] = ans[p] + 1;
      }
    }
  }
  //for(int i=0;i<n;i++)cout << u[i] << endl;
   
  for(int i=0;i<n;i++){
    cout << i+1 << " " << ans[u[i]] << endl;
  }
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
static const int INF=(1<<21);
int m[101][101],n;
int d[101]; //最短距離を記録
void bfs(int s){
  queue<int>q;
  q.push(s); //始点sを入れる（訪問）
  for(int i=0;i<n;i++) d[i]=INF;//予めINFを入れておく
  d[s]=0;
  while(!q.empty()){
    int u=q.front();
    q.pop(); //キューから頂点uを取り出し、訪問
    for(int v=0;v<n;v++){
      if(m[u][v]==0) continue;
      //辺がない
      if(d[v]!=INF) continue;
      //訪問したことがある
      d[v]=d[u]+1; //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(){
  int u,k,v;
  scanf("%d",&n);
   for(int i=0;i<n;i++){
    scanf("%d %d",&u,&k);
    u--;
    for(int j=0;j<k;j++){
      scanf("%d",&v);
      v--;
      m[u][v]=1;
    }
  }
   bfs(0);
   return 0;
}