Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n+1)
	d := make([]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}

		d[i+1] = -1
	}

	q := [][]int{}
	q = append(q, []int{1, 0})
	for len(q) > 0 {
		pop := q[0]
		if len(q) > 1 {
			q = q[1:]
		} else {
			q = [][]int{}
		}
		if d[pop[0]] >= 0 {
			continue
		}
		d[pop[0]] = pop[1]

		for j := 0; j < len(g[pop[0]]); j++ {
			q = append(q, []int{g[pop[0]][j], pop[1]+1})
		}
	}

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

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define rep3(i, s, n) for (int i = s; i > (int)(n); i--)
#define all(obj) obj.begin(), obj.end()
#define db(x) cerr << #x << ":" << x << " "
#define dbl(x) cerr << #x << ":" << x << "\n"
#define dbv(vec) cerr << #vec << ":"; for (auto e : vec) cerr << e << " "; cout << "\n"
#define dbvv(vv) cerr << #vv << ":\n"; for (auto vec : vv) { for (auto e : vec) cerr << e << " "; cerr << "\n"; }
#define dbm(map) cerr << #map << ":"; for (auto p : map) cerr << "(" << p.first << ", " << p.second << "), "; cerr << "\n";
#define YN(f) cout << (f ? "YES" : "NO") << "\n"
#define Yn(f) cout << (f ? "Yes" : "No") << "\n"
#define yn(f) cout << (f ? "yes" : "no") << "\n"
using ll = long long;
using vi = vector<int>; using vl = vector<ll>; using vs = vector<string>; using vb = vector<bool>;
using vvi = vector<vi>; using vvl = vector<vl>; using vvb = vector<vb>;
using pii = pair<int, int>; using pll = pair<ll, ll>;
using vpii = vector<pii>; using vpll = vector<pll>;
using ti3 = tuple<int, int, int>; using tl3 = tuple<ll, ll, ll>;
using mii = map<int, int>; using mil = map<int, ll>; using mli = map<ll, int>; using mll = map<ll, ll>;

int main() {
	int n; cin >> n;
	vvi g(n + 1); vi d(n + 1, -1); // 1-ind
	rep2(i, 1, n + 1) {
		int _, k; cin >> _ >> k;
		rep(j, k) {
			int v; cin >> v;
			g[i].push_back(v);
		}
	}
	// dbvv(g);
	queue<int> q;
	q.push(1); d[1] = 0;
	while (!q.empty()) {
		int u = q.front(); q.pop();
		for (int v : g[u]) {
			if (d[v] != -1) continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}
	rep2(i, 1, n + 1) printf("%d %d\n", i, d[i]);
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cmath>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
template<class T> inline bool chmax(T& a, T b) {if (a<b) {a=b; return true;} return false;}
template<class T> inline bool chmin(T& a, T b) {if (a>b) {a=b; return true;} return false;}

int main() {
  ll n; cin >> n;
  vvll edges(n, vll());
  REP(i, n) {
    ll u, k; cin >> u >> k;
    REP(j, k) {
      ll to; cin >> to;
      edges.at(u-1).push_back(to-1);
    }
  }
  vll d(n, -1);
  V<bool> visited(n, false);
  queue<ll> q; q.push(0);
  ll tmpd = 0;
  while (!q.empty()) {
    queue<ll> tmpq;
    while (!q.empty()) {
      ll to = q.front(); q.pop();
      if (visited.at(to)) continue;
      visited.at(to) = true; d.at(to) = tmpd;
      REP(i, edges.at(to).size()) {
        if (visited.at(edges.at(to).at(i))) continue;
        tmpq.push(edges.at(to).at(i));
      }
    }
    q = tmpq; tmpd++;
  }
  REP(i, n) cout << i+1 << ' ' << d.at(i) << endl;
  return 0;
}

C++: #include <queue>
#include <iostream>
#include <stdio.h>
#include <algorithm>

using namespace std;

int map_max;
int map[110][110];
int rows;
int cols[110];
int howfar[110];
int far;
int visited[110];

void get_map();
void print_map();
int search(int node);
void print_result();
void check_result();

int main()
{

  get_map();
  //print_map();
  for(int i=0;i<map_max;i++)
    {
      visited[i]=1000;
    }
  search(1);
  check_result();
  print_result();
  return 0;
}

void check_result()
{
  for(int i=1;i<map_max;i++)
    {
      if(visited[i]==1000)
	{
	  visited[i]=-1;
	}
    }
}

void print_result()
{
  for(int i=0;i<map_max;i++)
    {
      std::cout << i+1 << " " << visited[i] << std::endl;
    }
}

void get_map()
{

  int k;
  scanf("%d", &map_max);//<100

  for(int i=0;i<map_max;i++)
    {
      scanf("%d", &k);
      scanf("%d", &cols[i]);
      for(int j=0;j<cols[i];j++)
	{
	  map[i][j]=0;
	  scanf("%d", &map[i][j]);
	}
    }

  
  return ;
}

void print_map()
{
  for(int i=0;i<map_max;i++)
    {
      for(int j=0;j<cols[i];j++)
	{
	  printf("%d", map[i][j]);
	}
      puts("");
    }
  return ;
}


int search(int node)
{
  far =1;
  int next =1, now,buf;
  visited[node-1]=1;
  
  queue<int> q;//push root node
  for(int i=0;i<cols[node-1];i++)
    {
      now = map[node-1][i];
      q.push(now);//
      visited[now-1]=visited[node-1]+1;
      
    }

  while(!q.empty())
  {
    next = q.front();
    q.pop();
    for(int i=0;i<cols[next-1];i++)
      {
	now = map[next-1][i];
	if(visited[now-1]==1000)
	  {
	    q.push(now);
	    visited[now-1]=min(visited[next-1]+1, visited[now-1]);
	  }
      }
  }

  return 0;
}

/*
  int search(int node, int far)
  {
  int now = node-1;
  if(visited[now] == 1)//searched
  {
  if(howfar[now]>far)
  {
  howfar[now]=far;
  }
  //return -1;
  }
  else
  {
  howfar[now]=far;
  visited[now]=1;

  //for(int i=0;i<cols[node-1];i++)
  //{
  //  //if(map[node-1][i]!=0)
  //  {
  //  search(map[node-1][i], far+1);
  //  }
  //}
  //return -1;
  }

  for(int i=0;i<cols[now];i++)
  {
  //if(map[now][i]!=0)
  {
  search(map[now][i], far+1);
  }
  }
    

  return -1;
  }
*/