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>
using namespace std;

#define N 101
#define WHITE 0
#define GRAY 1
#define BLACK 2

int Q[N], M[N][N], color[N], d[N], f = 1, c = 1;

void Enqueue(int n) {
  Q[f] = n;
  f++;
  if ( f == N ) f = 1;
}

int Dequeue() {
  int x = c;
  c++;
  if ( c == N ) c = 1;
  return Q[x];
}

void BFS ( int s ) {
  int u;
  for ( int i = 1; i <= N; i++ ) {
    color[i] = WHITE;
    d[i] = -1;
  }
  color[s] = GRAY;
  d[s] = 0;
  Enqueue(s);
  while(f!=c ) {
    u = Dequeue();
    for ( int i = 1; i <= N; i++ ) {
      if ( color[i] == WHITE && M[u][i] == 1 ) {
	color[i] = GRAY;
	d[i] = d[u] + 1;
	Enqueue(i);
      }
    }
    color[u] = BLACK;
  }
}

int main() {
  int n,u,k,v;
  for ( int i = 1; i < N; i++ ) {
    for ( int j = 1; j < N; j++ ) {
      M[i][j] = 0;
    }
  }
  cin >> n;
  for ( int i = 1; i <= n; i++ ) {
    cin >> u >> k;
    for ( int j = 0; j < k; j++ ) {
      cin >> v;
      M[u][v] = 1;
    }
  }
  BFS(1);
  for ( int i = 1; i <= n; i++ ) {
    cout << i << " " << d[i] << endl;
  }
  return 0;
}

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

int G[123][123];
int dis[123];
int n;

struct st {
  int pos;
  int v;
};

int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for(int i = 1; i <= n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    for(int j = 0; j < v; j++) {
      int t;
      scanf("%d", &t);
      G[u][t] = G[t][u] = 1;
    }
  }

  memset(dis, 0x3f, sizeof dis);

  int start = 1;
  queue<st> q;
  q.push((st) {
    1, 0
  });

  while(!q.empty()) {

    st u = q.front();
    q.pop();
    dis[u.pos] = u.v;

    for(int i = 1; i <= n; i++) {
      if(i == u.pos) continue;
      if(G[i][u.pos] == 0) continue;
      if(dis[i] <= u.v) continue;
      q.push((st){i, u.v + 1});
    }
  }

  for(int i=1;i<=n;i++) {
    printf("%d %d\n", i, dis[i] > 100000 ? -1 : dis[i]);
  }

  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

const int INF = 1e9;

int main(void) {
    int n, u, k ,v, G[110][110] = {}, d[110];
    cin >> n;

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

    fill(d, d + 110, INF);
    d[1] = 0;

    queue<int> q;
    q.push(1);
    while (q.size()) {
        int f = q.front(); q.pop();

        for (int i = 1; i <= n; i++) {
            if (!G[f][i] || d[i] != INF) continue;
            d[i] = d[f] + 1;
            q.push(i);
        }
    }

    for (int i = 1; i <= n; i++)
        cout << i << " " << ((d[i] == INF) ? -1 : d[i]) << endl;

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
#include "../cxx-prettyprint/prettyprint.hpp"


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

    std::unordered_map<int, std::vector<int>> Adj(n);

    for (int i = 0; i < n; i ++) {
        int u, k;
        std::cin >> u >> k;
        std::vector<int> V(k);
        for (int& v : V) {
            std::cin >> v;
        }
        Adj[u] = V;
    }

    std::set<int> nodes_unvisited;
    std::unordered_map<int, int> distances;

    for (int i = 1; i <= n; i ++) {
        nodes_unvisited.insert(i);
    }

    nodes_unvisited.erase(1);
    distances[1] = 0;
    std::queue<int> queue;
    queue.push(1);

    while (!queue.empty()) {
        int u = queue.front();
        queue.pop();
        for (int v : Adj[u]) {
            if (nodes_unvisited.find(v) != nodes_unvisited.end()) {
                nodes_unvisited.erase(v);
                distances[v] = distances[u] + 1;
                queue.push(v);
            }
        }
    }

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

C++: #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define white 0
#define gray 1
#define black 2


int n;
int M[100][100];
int d[100],color[100];

void bfs(int u) {
  
  queue<int> Q;
  Q.push(u);
  color[u] = gray;
  while(!Q.empty()) {
    int q = Q.front();
    for(int i = 0;i < n;i++) {
      if(M[q][i] && color[i] == white) {
        Q.push(i);
        d[i] = d[q] + 1;
        color[i] = gray;
      }
    }
    Q.pop();
    color[q] = black;
  }
}

int main() {


  cin >> n;

  for(int i = 0;i < n;i++) {
    d[i] = 0;
    color[i] = white;
    for(int j = 0;j < n;j++) {
      M[i][j] = 0;
    }
  }
  

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

  bfs(0);
  
  for(int i = 0;i < n;i++) {
    cout << i+1 << " ";
    if(d[i] || i == 0) cout << d[i] << endl;
    else cout << -1 << endl;
  }

}

C++: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
int n;
int D[100];
int matrix[100][100];
void bfs(int src)
{
    cerr<<"bfs root = "<<src<<endl;
    queue<int> q;
    q.push(src);
    D[src]=0;
    while(!q.empty())
    {
        int cur=q.front();
        q.pop();
        cerr<<"visiting "<<cur<<' '<<D[cur]<<endl;
        for(int dst=0;dst<n;dst++)
        {
            if(matrix[cur][dst]==1&&D[dst]==99999999)
            {
                D[dst]=D[cur]+1;
                q.push(dst);
            }
        }
    }
};

int main()
{
    vector<int> l[101];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int u,k;
        cin>>u>>k;
        for(int j=0;j<k;j++)
        {
            int tmp;
            cin>>tmp;
            l[u].push_back(tmp);
        }
    }
    
    for(int i=0;i<100;i++)
    {
        D[i]=99999999;
    }
    
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            matrix[i][j]=0;
        }
    }
    
    for(int i=1;i<n+1;i++)
    {
        for(int j=0;j<l[i].size();j++)
        {
            matrix[i-1][l[i][j]-1]=1;
        }
    }
    
    for(int i=0;i<n;i++)
    {
        bfs(i);
    }
    
    for(int i=0;i<n;i++)
    {
        cout<<(i+1)<<' '<<D[i]<<endl;
    }
}