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<string>
#include<algorithm>
#include<utility>
#include<numeric>
#include<vector>
#include<map>
#include<set>
#include<tuple>
#include<stack>
#include<queue>
#include<functional>
#include<iterator>
#include<cmath>

using namespace std;

typedef pair<int,int> P;


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

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

	int d[n];
	fill(d,d+n,-1);
	queue<P> que;
	que.push(P(0,0)); //点番号（0オリジン）、距離
	d[0] = 0;
	while(!que.empty()){
		P t = que.front(); que.pop();
		for(int i=0;i<es[t.first].size();i++){
			if(d[es[t.first][i]] == -1){
				que.push(P(es[t.first][i],t.second+1));
				d[es[t.first][i]] = t.second+1;
			}
		}
	}

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

C++: #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

#define MOD 1000000007

vector<int> g[100];

int main() {
  int n;
  cin >> n;
  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--;
      g[u].push_back(v);
    }
  }
  vector<int> d(n, 1e9);
  queue<int> que;
  que.push(0);
  d[0] = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto u : g[v]) {
      if (d[u] == 1e9) {
        que.push(u);
        d[u] = d[v] + 1;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    cout << i + 1 << " " << (d[i] == 1e9 ? -1 : d[i]) << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
const ll mod = 1000000007;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
ll factorial(ll n) {
  if (n == 1) {
    return 1;
  }
  ll ans = factorial(n-1);
  return ans*n;
}
ll gcd(ll a, ll b) {
  if (a < b) {
    ll tmp = a;
    a = b;
    b = tmp;
  }
  ll r = a%b;
  while(r != 0) {
    a = b;
    b = r;
    r = a%b;
  }
  return b;
}
ll lcm(ll a, ll b) {
  return (a/gcd(a, b))*b;
}

int main() {
  ll n;
  cin >> n;
  vector <vector <bool>> v(n, vector <bool> (n, 0));
  vector <ll> color(n, 0);
  vector <ll> d(n, INF);
  queue <ll> q;
  rep(i, n) {
    ll a, b, c;
    cin >> a >> b;
    rep(j, b) {
      cin >> c;
      v.at(a-1).at(c-1) = 1;
    }
  }
  color.at(0) = 1;
  d.at(0) = 0;
  q.push(0);
  while (!q.empty()) {
    ll i = q.front();
    q.pop();
    rep(j, n) {
      if (v.at(i).at(j) && color.at(j)==0) {
        color.at(j) = 1;
        d.at(j) = d.at(i) + 1;
        q.push(j);
      }
    }
    color.at(i) = 2;
  }
  rep(i, n) {
    if (d.at(i) == INF) {
      cout << i+1 << ' ' << -1 << endl;
    } else {
      cout << i+1 << ' ' << d.at(i) << endl;
    }
  }
}

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

using namespace std;

int main(){
  queue<int> q;
  int n,k,u;
  int c[101];
  vector<int>  d[101];
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    d[u].resize(k);
    for(int j=0;j<k;j++) cin>>d[u][j];
  }
  for(int i=0;i<=n;i++) c[i]=-1;
  q.push(1);
  c[1]=0;
  int a;
  while(!q.empty()){
    a=q.front(),q.pop();
    for(int i=0;i<d[a].size();i++)
      if( d[a][i]>1 && c[d[a][i]]==-1){
	q.push(d[a][i]);
	c[d[a][i]]=c[a]+1;
      }
  }
  for(int i=1;i<=n;i++) cout<<i<<" "<<c[i]<<endl;
  return 0;
}

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

struct Node {
	int d = -1;
	bool flag = false;
	vector<int> v;
};

queue<int> s;

void bfs_s(vector<Node>& u, int id, int d)
{
	if (u[id].flag) return;

	u[id].d = d;
	u[id].flag = true;
	for (int i = 0; i < u[id].v.size(); i++)
	{
		s.push(u[id].v[i]);
	}
	return;
}

void bfs(vector<Node>& u)
{
	int d = 0;
	while(!s.empty())
	{
		int n = s.size();
		for (int i = 0; i < n; i++)
		{
			bfs_s(u, s.front(), d);
			s.pop();
		}
		d++;
	}
	return;
}

int main(void)
{
	int N, id, k, x;
	Node z;
	cin >> N;
	vector<Node> u(N + 1);
	for (int i = 0; i < N; i++)
	{
		cin >> id >> k;
		for (int j = 0; j < k; j++)
		{
			cin >> x;
			u[id].v.push_back(x);
		}
	}
	s.push(1);
	bfs(u);

	for (int i = 1; i <= N; i++) cout << i << " " << u[i].d << endl;

	return 0;
}

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

using namespace std;

vector<int> graph[100];
int dist[100]={0};
int main(){
  int n;
  cin >> n;

  for(int i=0;i<n;++i){
    int u,k;
    cin >> u >> k;
    for(int i=0;i<k;++i){
      int v;
      cin >> v;
      graph[u-1].push_back(v-1);
    }
    dist[i] = -1;
  }

  queue<int> q;
  q.push(0);
  dist[0] = 0;
  while(!q.empty()){
    auto v = q.front();q.pop();
    for(auto next_v:graph[v]){
      if(dist[next_v]==-1){
        dist[next_v] = dist[v]+1;
        q.push(next_v);
      }
    }
  }
  for(int i=0;i<n;++i){
    cout << i+1 << " " << dist[i] << endl;
  }
}