Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type AdjacentList struct {
	matrix [][]int
}

func NewEmptyAdjacentList(dim int) *AdjacentList {
	matrix := make([][]int, dim)
	for i := 0; i < dim; i++ {
		matrix[i] = make([]int, dim)
	}
	return &AdjacentList{matrix: matrix}
}

func (al *AdjacentList) set(node int, adjacent int) {
	al.matrix[node-1][adjacent-1] = 1
}

func (al *AdjacentList) findAdjacentsFor(node int) []int {
	adjacents := []int{}
	for i, v := range al.matrix[node-1] {
		if v == 1 {
			adjacents = append(adjacents, i+1)
		}
	}
	return adjacents
}

type DistanceList struct {
	list []int
}

func NewDistanceList(dim int) *DistanceList {
	list := make([]int, dim)
	for i := 0; i < dim; i++ {
		list[i] = -1
	}
	return &DistanceList{list: list}
}

func (dl *DistanceList) set(node int, distance int) {
	dl.list[node-1] = distance
}

func (dl *DistanceList) get(node int) int {
	return dl.list[node-1]
}

func (dl *DistanceList) isVisited(node int) bool {
	return dl.list[node-1] >= 0
}

type Position struct {
	node     int
	distance int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	dim, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	al := NewEmptyAdjacentList(dim)

	for i := 1; i <= dim; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}

		adjacentNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		for j := 0; j < adjacentNum; j++ {
			adjacent, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			al.set(node, adjacent)
		}
	}

	dl := NewDistanceList(dim)

	bfs(al, dl)

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

func bfs(al *AdjacentList, dl *DistanceList) {
	stack := []Position{Position{node: 1, distance: 0}}
	for len(stack) > 0 {
		currentPosition := stack[0]
		stack = stack[1:]
		if !dl.isVisited(currentPosition.node) {
			dl.set(currentPosition.node, currentPosition.distance)
		}
		for _, v := range al.findAdjacentsFor(currentPosition.node) {
			if !dl.isVisited(v) {
				stack = append(stack, Position{node: v, distance: currentPosition.distance + 1})
			}
		}
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

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;
  }
*/