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++: /**
 * @Author: yujirotakahashi
 * @Date:   2020-05-17T20:21:41+09:00
 * @Last modified by:   yujirotakahashi
 * @Last modified time: 2020-05-24T01:58:29+09:00
 */

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

/*******  All Required define Pre-Processors and typedef Constants *******/
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define rep(i,n) for(int i=0; i<(n); ++i)
#define rep1(i,n) for(int i=1; i<=(n); ++i)
#define Dcout(a) cout << setprecision(20) << a << endl
#define MP make_pair
#define PB push_back
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
//const ll INF = (1LL << 60);
//const double pi=acos(-1.0);
using ll = long long;
using PAIR = pair<int, int>;
#define debug cout << "line : " << __LINE__ << " debug" << endl
/****** Template of some basic operations *****/
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; }
/**************************************/

/******** User-defined Function *******/
using Graph = vector<vector<int>>;

/**************************************/

/********** Main()  function **********/
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n; cin >> n;
  Graph G(n);
  rep(i,n){
      int u,k; cin >> u >> k;
      u--;
      rep(j,k){
          int v; cin >> v;
          v--;
          G[u].push_back(v);
      }
  }

  vector<int> dist(n,-1);
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  while(!que.empty()){
      int v = que.front(); que.pop();
      for(int nv: G[v]){
          if(dist[nv] != -1) continue;
          dist[nv] = dist[v] + 1;
          que.push(nv);
      }
  }

  rep(i,n){
      cout << i+1 << " " << dist[i] << endl;
  }
  return 0;
}
/********  Main() Ends Here *************/

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

vector<vector<ll>> lis;
vector<ll> dset;
ll d=0;

void find(ll p) {
    if(dset[p] == -1 || dset[p] > d) {
        dset[p] = d;
        d++;
        for(ll i:lis[p]) {
            find(i);
        }
        d--;
    }
}

int main() {
    ll n;
    cin >> n;
    for(ll i=0;i<n;i++) {
        ll u,k;
        cin>>u>>k;
        vector<ll> aaa;
        for(ll j=0;j<k;j++) {
            ll tmp;
            cin>>tmp;
            aaa.push_back(tmp-1);
        }
        lis.push_back(aaa);
        dset.push_back(-1);
    }
    find(0);
    for(ll i=0;i<n;i++) {
        cout << i+1<<" "<<dset[i]<<endl;
    }
}

C++: #include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <deque>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <unordered_map>
#include <bitset>
#include <sstream>
#define mop 1000000007

using namespace std;

int m[101][101] = {};
int d[101] = {};
int n;
int INFTY = (1 << 15);
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=1;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=1;i<=n;i++){
        cout << i << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
    }
}
int main() {
    
    cin >> n;
    for (int i=1;i<=n;i++){
        int u,k;
        cin >> u >> k;
        for (int j = 0;j<k;j++){
            int p;
            cin >> p;
            m[i][p] = 1;
           
        }
    }
    bfs(1);

}

C++: #include<iostream>
#include<vector>
#include<deque>
#include<tuple>
using namespace std;

int main(){
  vector<int> ans;
  vector<vector<int> > path_field;
  int n;

  cin >> n;
  n++;
  path_field.resize(n);
  ans.resize(n);
  for(int i=1;i<n;i++){
    ans[i]=-1;
  }
  for(int i=1;i<n;i++){
    int _m,m;

    path_field[i].resize(n);
    for(int j=1;j<n;j++){
      path_field[i][j]=0;
    }

    cin >> _m >> m;
    for(int j=0;j<m;j++){
      int to;
      
      cin >> to;
      path_field[i][to]=1;
    }
  }

  deque<tuple<int,int> > que;

  que.push_back(make_tuple(1,0));
  ans[1]=0;
  while(!que.empty()){
    int& standingby=get<0>(que.front());

    for(int i=1;i<n;i++){
      if((path_field[standingby][i]==1)&&(ans[i]==-1)){
        que.push_back(make_tuple(i,get<1>(que.front())+1));
	ans[i]=get<1>(que.front())+1;
      }
    }
    que.pop_front();
  }

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

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

int main(){
  int n;
  cin>>n;
  vector<vector<int>> g(n);
  for(int i=0;i<n;i++){
   int I,k;
    cin>>I>>k;
    for(int j=0;j<k;j++){
      int x;
      cin>>x;
      x--;
      g[i].push_back(x);
    }
  }
  
  	vector<int> d(n,-1);
 	 queue<int> q;
  
 	 d[0]=0;
 	 q.push(0);
  
 	 while(!q.empty()){
  	  int v=q.front(); q.pop();
     
  	  for(int &nv: g[v]){
    	  if(d[nv]!=-1) continue;
       
    	  d[nv]=d[v]+1;
    	  q.push(nv);
      }
     }
  for(int i=0;i<n;i++) cout<<i+1<<" "<<d[i]<<endl;
}