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 <bits/stdc++.h>
#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const double pi = 3.141592653589793;
#define MOD 1000000007
#define INF 10000000000000


signed main(){
  int u,k,id;
  cin>>u;
  vector<vector<int> > v(u,vector<int>(0));
  rep(i,u){
    cin>>id>>k;
      rep(j,k){
        int t;cin>>t;
        v[id-1].push_back(t);
  }
}
queue<int>q;
q.push(1);
vector<int>ans(u);
rep(i,u){ans[i]=-1;}
ans[0]=0;
int f;
while(!q.empty()){
  f=q.front();
  q.pop();
  rep(i,v[f-1].size()){
    if(ans[v[f-1][i]-1]<0){
    ans[v[f-1][i]-1]=ans[f-1]+1;
    q.push(v[f-1][i]);
  }
  }
}
  rep(i,u){
    cout<<i+1<<' '<<ans[i]<<endl;
  }
}

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

int n;
vector <int> G[101];
queue <int> Q;
int  d[100];
int  dis[101];


int visit(int x){
  int t,i;
  
  Q.push(x);
  dis[x] = 0;
  d[x] = 1;

  while(Q.empty() == false) {
    
    // cout << "start Q" <<endl;

    t = Q.front();
    Q.pop();

    for(i=0; i<G[t].size();i++) {
      if(d[G[t][i]] == 0 ) {
	d[G[t][i]] = 1;
	dis[G[t][i]] = dis[t] + 1;

	Q.push(G[t][i]);
      }

    }
  }
}
    
int main() {
  
  int i,j,k,l,u,a,t;
  
  
  scanf("%d",&n);

  
  
  for(i=0;i<n;i++){
    d[i] = 0;
    dis[i] = -1;
    scanf("%d %d ",&k,&u);
    
    for(j=0;j<u;j++) {
      scanf("%d",&a);
      G[k-1].push_back(a-1);
      // printf("G[%d][%d] = 1\n",i,k-1);
    }
  }
  
  
  visit(0);

  

  for(i=0;i<n;i++) cout << i+1<<" " << dis[i] <<endl;



  return 0;

    
    
}

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

using namespace std;

const int maxn=1e2+5;
const int inf=(1<<21);

int n,m[maxn][maxn];
int d[maxn];

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(m[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()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            m[i][j]=0;
        }
    }
    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--;
            m[u][v]=1;
        }
    }
    bfs(0);
    return 0;
}
/*
4
1 2 2 4
2 1 4
3 0
4 1 3
*/

C++: 
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <ctime>
#include <string>
#include <queue>

using namespace std;

const int MAX_N = 100;
int n;
int suji[MAX_N];
int	num[MAX_N];
int adj[MAX_N][MAX_N];
int ans[MAX_N][MAX_N];
int D[MAX_N];

void bfs(int src){
	queue<int> Q;
	Q.push(src);
	D[src] = 0;

	while(!Q.empty()){
		int cur = Q.front();
		Q.pop();

		for(int i = 1; i < n; i++){
			if(ans[cur][i] == 1 && D[i] == -1){
				D[i] = D[cur] + 1;
				Q.push(i);
			}
		}
	}
}


int main(void){

	cin >> n;
	for(int i = 0; i < n; i++){
		
		cin >> suji[i] >> num[i];
		D[i] = -1;
		
		for(int j = 0; j < num[i]; j++){
			cin >> adj[i][j];
			adj[i][j] -= 1;
		}
	}

	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			ans[i][j] = 0;
		}
	}

	for(int i = 0; i < n; i++){
		for(int j = 0; j < num[i]; j++){
			ans[i][adj[i][j]] = 1;
		}
	}

	bfs(0);

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

C++: #include <iostream>
#include <vector>
#include <list>
#include <cstdlib>
#include <climits>

const int MAX_N = 100;

struct Node {
  int v;
  int d;
  bool visited;
};

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);  

  int n = 0;
  std::cin >> n;

  std::vector<std::vector<int> *> *graph = new std::vector<std::vector<int> *>(n + 1);
  std::vector<struct Node *> *nodes = new std::vector<struct Node *>(n + 1);
  
  for (int i = 1; i <= n; i++) {
    int v = 0;
    std::cin >> v;

    int k = 0;
    std::cin >> k;

    std::vector<int> *neighbors = new std::vector<int>(k);

    for (int i = 0; i < k; i++) {
      int a = 0;
      std::cin >> a;

      neighbors->at(i) = a;
    }

    graph->at(i) = neighbors;

    struct Node *node = (struct Node *) std::malloc(sizeof(struct Node));

    node->v = i;
    node->d = INT_MAX;
    node->visited = false;
    
    nodes->at(v) = node;
  }

  int count = 0;
  int start = 1;

  std::list<int> *stack = new std::list<int>();

  stack->push_back(nodes->at(start)->v);

  while (!stack->empty()) {
    std::vector<int> *newNodes = new std::vector<int>();

    while (!stack->empty()) {    
      int v = stack->front();
      stack->pop_front();

      if (node->d < count) {
	nodes->at(v)->d = count;
      }
      
      nodes->at(v)->visited = true;
      
      std::vector<int> *neighbors = graph->at(v);

      for (std::vector<int>::iterator iterator = neighbors->begin(); iterator != neighbors->end(); ++iterator) {
	struct Node *node = nodes->at(*iterator);

	if (!node->visited) {
	  newNodes->push_back(node->v);
	}
      }
    }

    for (std::vector<int>::iterator iterator = newNodes->begin(); iterator != newNodes->end(); ++iterator) {
      stack->push_front(*iterator);
    }

    count++;
  }

  for (std::vector<struct Node *>::iterator iterator = nodes->begin() + 1; iterator != nodes->end(); ++iterator) {
    struct Node *node = *iterator;

    if (node->d == INT_MAX) {
      node->d = -1;
    }
        
    std::cout << node->v << " " << node->d << std::endl;
  }
  
  return 0;
}