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 <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

// Acknowledgement: Special thanks to kyomukyomupurin, who developed this
// template.
template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

template <class T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
  int n = 0;
  for (auto e : vec) os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& st) {
  int n = 0;
  for (auto e : st) os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::map<T, U>& mp) {
  int n = 0;
  for (auto e : mp) os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
  for (T& e : vec) is >> e;
  return is;
}

#ifdef LOCAL
#define debug(...) \
  std::cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out() { std::cerr << '\n'; }

template <class Head, class... Tail>
void debug_out(Head&& head, Tail&&... tail) {
  std::cerr << head;
  if (sizeof...(Tail) != 0) std::cerr << ", ";
  debug_out(std::forward<Tail>(tail)...);
}
using namespace std;
using int64 = long long;

void bfs(int start_node_id, const vector<vector<int>>& g,
         vector<int>& visit_status, vector<int>& distance, vector<int>& parent,
         int col = 1) {
  int node_id;
  queue<int> q;
  visit_status[start_node_id] = col;
  distance[start_node_id] = 0;
  q.push(start_node_id);
  while (!q.empty()) {
    node_id = q.front();
    q.pop();
    for (int i = 0; i < (int)g[node_id].size(); i++) {
      int next_node_id = g[node_id][i];
      if (visit_status[next_node_id] == 0) {
        q.push(next_node_id);
        visit_status[next_node_id] = col;
        distance[next_node_id] = distance[node_id] + 1;
        parent[next_node_id] = node_id;
      }
    }
    visit_status[node_id] = col;
  }
}

int main() {
  int n, k, u, v;
  cin >> n;
  vector<vector<int>> g(n);
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v;
      g[i].push_back(v - 1);
    }
  }
  debug(g);
  vector<int> visit_status(n), distance(n, -1), parent(n);
  bfs(0, g, visit_status, distance, parent);
  for (int i = 0; i < n; i++) {
    cout << i + 1 << ' ' << distance[i] << endl;
  }
  return 0;
}

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

int AdjM[101][101]={0};
int D[101];

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

void bfs(int s){
    //cerr<<"bfs root "<<s<<"\n";
    queue<int> Q;
    Q.push(s);
    D[s]=0;
    while(!Q.empty()){
        int cur=Q.front();
        Q.pop();
        //cerr<<"visiting "<<cur<<" "<<D[cur]<<"\n";
        for(int i=1;i<101;++i){
            if(AdjM[cur][i]!=0 && D[i]==-1){
                D[i]=D[cur]+1;
                Q.push(i);
            }
        }
    }
}

int main(){
    Fill(D,-1);
    int n;
    cin>>n;
    int id,num,ix;
    for(int i=0;i<n;++i){
        cin>>id>>num;
        for(int j=1;j<=num;++j){
            cin>>ix;
            AdjM[id][ix]=1;
        }
    }

    bfs(1);

    for(int i=1;i<=n;++i){
        cout<<i<<" "<<D[i]<<"\n";
    }
}

C++: //
//  main.cpp
//  breadth_first_search
//
//  Created by ???????????? on 2017/10/22.
//  Copyright ?? 2017 ????????????. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <queue>

#define n_max 100
#define INFTY 1000
#define WHITE 0
#define GRAY 1
#define BLACK 2

using namespace std;

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

queue<int> Q;

void encode_graph(){
    int i, j, id, num, node;
    
    //initialize with 0
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            M[i][j] = 0;
        }
    }
    
    for (i = 0; i < n; i++){
        scanf("%d", &id);
        scanf("%d", &num);
        for(j = 0; j < num; j++){
            scanf("%d", &node);
            M[id-1][node-1] = 1;
        }
    }
}

void bfs(){
    for(int i = 0; i < n ; i++){
        color[i] = WHITE;
        d[i] = INFTY;
    }
    //starting point
    int s = 0;
    color[s] = GRAY;
    d[s] = 0;
    Q.push(s);
    
    while(!Q.empty()){
        int u = Q.front(); Q.pop();
        for(int v = 0; v < n; v++){
            if((M[u][v]) && (color[v] == WHITE)){
                color[v] = GRAY;
                d[v] = d[u] + 1;
                Q.push(v);
            }
        }
        color[u] = BLACK;
    }
    
    
    
}

int main(int argc, const char * argv[]) {
    scanf("%d", &n);
    
    encode_graph();
    bfs();
    
    for(int i = 0; i < n; i++){
        cout<<i+1<<" "<<d[i]<<endl;
    }
    
//    std::cout << "Hello, World!\n";
    return 0;
}

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

int main()
{
  int n;
  cin >> n;
  Graph G(n + 1);
  int u, k, v;
  for (int i = 0; i < n; i++)
  {
    cin >> u >> k;
    for (int j = 0; j < k; j++)
    {
      cin >> v;
      G.at(u).push_back(v);
    }
  }
  vector<int> dist(n + 1, -1);
  queue<int> que;
  dist.at(1) = 0;
  que.push(1);
  int nv;
  while (!que.empty())
  {
    v = que.front();
    que.pop();
    for (int i = 0; i < G.at(v).size(); i++)
    {
      nv = G.at(v).at(i);
      if (dist.at(nv) != -1)
      {
        continue;
      }
      dist.at(nv) = dist.at(v) + 1;
      que.push(nv);
    }
  }
  for (int i = 1; i <= n; i++)
  {
    cout << i << " " << dist.at(i) << endl;
  }
}