Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
)

const MaxNode int = 100

var d []int
var A [][]int
var N int
var l *list.List

func bfs(u int) {
	d[u] = 0
	l.PushBack(u)
	for ;l.Len() != 0; {
		u = l.Front().Value.(int)
		l.Remove(l.Front())
		for v:=0; v<N; v++ {
			if A[u][v] == 0 {
				continue
			}
			if d[v] != -1 {
				continue
			}
			d[v] = d[u] + 1
			l.PushBack(v)
		}
	}
}


func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func print() {
	for i:=0; i<N; i++ {
		fmt.Printf("%d %d\n", i+1, d[i])
	}
}

func initVariable() {
	d = make([]int, MaxNode)
	A = make([][]int, MaxNode)
	l = list.New()
	for i:=0; i<MaxNode; i++ {
		A[i] = make([]int, MaxNode)
		d[i] = -1
	}
}


func main() {
	initVariable()
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	for i:=0; i<N; i++ {
		idx := nextInt(scanner)
		cnt := nextInt(scanner)
		for j:=0; j<cnt; j++ {
			node := nextInt(scanner)
			A[idx-1][node-1] = 1 
		}
	}
	bfs(0)
	print()
}

C++: #include <algorithm>
#include <iostream>
#include <list>
#include <vector>

class TNode
{
	std::list<TNode*> FNexts;
	int  FDistance;
public:
	TNode()
	: FDistance(-1)
	{
	}
	
	int GetDistance()const {return FDistance;}
	
	void Push(TNode* New)
	{
		FNexts.push_back(New);
	}
	
	void Visit(int Distance)
	{
		if(FDistance >= 0 && Distance > FDistance) return;
		FDistance = Distance;
		for(std::list<TNode*>::iterator It = FNexts.begin(); It != FNexts.end(); ++It){
			(*It)->Visit(Distance + 1);
		}
	}
};

class TGraph
{
	std::vector<TNode*> FNodes;
public:
	TGraph(int Count)
	: FNodes(Count + 1)
	{
		for(int i = 1; i < Count + 1; ++i){
			FNodes[i] = new TNode();
		}
	}
	
	~TGraph()
	{
		for(unsigned int i = 1; i < FNodes.size(); ++i){
			delete FNodes[i];
		}
	}
	
	void InputNode()
	{
		int Index;
		std::cin >> Index;
		int BranchCount;
		std::cin >> BranchCount;
		for(int i = 0; i < BranchCount; ++i){
			int Target;
			std::cin >> Target;
			FNodes[Index]->Push(FNodes[Target]);
		}
	}
	
	void BreathFirstSearch()
	{
		FNodes[1]->Visit(0);
	}
	
	int Distance(int Target)const
	{
		return FNodes[Target]->GetDistance();
	}
};

int main()
{
	int Count;
	std::cin >> Count;
	TGraph Graph(Count);
	for(int i = 0; i < Count; ++i){
		Graph.InputNode();
	}
	Graph.BreathFirstSearch();
	
	for(int i = 0; i < Count; ++i){
		std::cout << i + 1 << " " << Graph.Distance(i + 1) << std::endl;
	}
	return 0;
}

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

int main() {
	int a[101][101] = {{0}};
	int n, u, k, v, km;
	km = 0;
	cin >> n;
	for ( int i = 0; i < n; i++ ) {
		cin >> u, k;
		for ( int j = 1; j <= k; j++ ) {
			cin >> v;
			a[u][v] = 1;
		}
		km++;
	}
	int n0;
	int b[101] = {- 1};
	queue<int> q;
	q.push( 1 );
	b[1] = 0;
	for ( int i = 1; i <= km; i++ ) {
		n0 = q.front();
		q.pop();
		for ( int j = 1; j <= km + 1; j++ ) {
			if ( a[n0][j] == 1 && b[j] == - 1 ) {
				q.push( j );
				b[j] = b[n0] + 1;
			}
			if ( q.empty() ) break;
		}
	}
	for ( int i = 1; i <= km; i++ )
		cout << i << " " << b[i] << endl;
}

C++: //Breadth First Search (?????????) Algorithm
#include<iostream>
#include<queue>
#define WH 0
#define GR 1
#define BL 2
#define INF 2000000
void bfs(int);
void init(void);
int n, *color, **M, *d;
std::queue<int> Q;
int main(){
  std::cin>>n;
  color=new int[n], d=new int[n], M=new int*[n];
  for(int i=0, u, k;i<n;i++){
    M[i]=new int[n];
    color[i]=WH, d[i]=INF;
    for(int j=0;j<n;j++) M[i][j]=0;
    std::cin>>u>>k;
    for(int j=0, v;j<k;j++){
      std::cin>>v;
      M[u-1][v-1]=1;
    }
  }
  bfs(0);
  for(int i=0;i<n;i++) std::cout<<i+1<<" "<<(d[i]!=INF?d[i]:-1)<<std::endl;
  delete color;
  delete d;
  for(int i=0;i<n;i++) delete M[i];
  delete M;
  return 0;
}

void bfs(int s){
  color[s]=GR, d[s]=0;
  Q.push(s);
  while(!Q.empty()){
    int u=Q.front();
    Q.pop();
    for(int i=0;i<n;i++){
      if(M[u][i]&&color[i]==WH){
        color[i]=GR, d[i]=d[u]+1;
        Q.push(i);
      }
    }
    color[u]=BL;
  }
}

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

#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
template<class T> using V = vector<T>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
const int inf = (1<<30)-1;
const ll infll = (1LL<<62)-1;
ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }



int main(){
    int n;
    cin >> n;
    V<V<int>> g(n);
    rep(i,n) {
        int a;
        cin >> a;
        a--;
        int k;
        cin >> k;
        rep(j,k) {
            int b;
            cin >> b;
            b--;
            g[a].push_back(b);
        }
    }

    V<int> d(n,-1);
    d[0] = 0;
    queue<int> q;
    q.push(0);
    while(sz(q)) {
        int v = q.front(); q.pop();
        for(int nv : g[v]) {
            if(d[nv] != -1) continue;
            d[nv] = d[v] + 1;
            q.push(nv);
        }
    }

    rep(i,n) {
        printf("%d %d\n", i+1, d[i]);
    }
}

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,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 <stdio.h>
#include <queue>
#include <algorithm>

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

using namespace std;

int n;
int M[N][N]; // ??£??\??????
int d[N]; // ???????????¢


void bfs()
{
    int i;
    for (i = 0; i < n; i++) {
        d[i] = -1;
    }
    
    int u;
    u = 0;
    d[u] = 0;
    queue<int> q;
    q.push(u);
    
    while (!q.empty()) {
        u = q.front();
        q.pop();
        for (i = 0; i < n; i++) {
            if (M[u][i] == 0) continue;
            q.push(i);
            if (d[i] == -1) {
                d[i] = d[u] + 1;
            }
        }
    }
    
    for (i = 0; i < n; i++) {
        printf("%d %d\n", i+1, d[i]);
    }
}


int main()
{
    scanf("%d", &n);
    int i, j;
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            M[i][j] = 0;
        }
    }
    
    // input graph
    int u, k, v;
    for (i = 0; i < n; i++) {
        scanf("%d %d", &u, &k);
        for (j = 0; j < k; j++) {
            scanf("%d", &v);
            M[u-1][v-1] = 1;
        }
    }
    
    bfs();
    
    return 0;
}