Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

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

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

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

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

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

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 <bits/stdc++.h>
using namespace std;

static const int N = 100;
static const unsigned int INF = (1 << 31);

int n,m[N][N];
int dist[N];

void bfs(int s){
    queue<int> q;
    q.push(s);
    for(int i=0;i<n;i++) dist[i] = INF;
    dist[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(dist[v] != INF) continue;
            dist[v] = dist[u]+1;
            q.push(v);
        }
    }
    for(int i=0;i<n;i++){
        cout << i+1 << ' ' << ((dist[i] == INF) ? -1 : dist[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);
}

C++: #include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for(int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
const long long INFLL = 1LL << 60;
const int INFINT = 1 << 30;
const long long MOD = 1e9 + 7;

vector<vector<int>> G(101, vector<int>(0));
vector<int> c(101, 0);
vector<int> d(101, 0);
vector<int> f(101, 0);

template <class T> void vecout(T V) {
    auto t = V.begin();
    *t++;
    while(t != V.end()) {
        cout << " " << *t++;
        // if(t != V.end()) cout << " ";
    }
    cout << endl;
};

int t = 0;
void dfs(int i) {
    if(c[i]) return;
    c[i] = 1;
    d[i] = ++t;
    for(auto e : G[i])
        dfs(e);
    f[i] = ++t;
}

class node {
  public:
    int dist = -1;
    vector<int> nex;
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, u, k, a;
    cin >> n;
    vector<node> Node(n + 1);
    queue<int> Q;
    rep(i, n) {
        cin >> u >> k;
        rep(j, k) {
            cin >> a;
            Node.at(u).nex.push_back(a);
        }
    }
    Node[1].dist = 0;
    Q.push(1);

    while(!Q.empty()) {
        int v = Q.front();
        Q.pop();
        for(auto e : Node[v].nex) {
            if(Node[e].dist > -1) continue;
            Node[e].dist = Node[v].dist + 1;
            Q.push(e);
        }
    }

    rep(i, 1, n + 1) cout << i << " " << Node[i].dist << "\n";
}

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

const int nIMPOS = -1;
struct StDist
{
  int m_nDist;
  int m_nDmy;

  StDist(int nDist) : m_nDist(nDist), m_nDmy(0)
  { }
};

void fnInput(vector<vector<int>>& rvvnAdjcy, vector<StDist>& rvoDist)
{
  int nxSiz;
  cin >> nxSiz;

  rvvnAdjcy.resize(nxSiz + 1, vector<int>(nxSiz + 1, 0));
  for (int i = 0; i < nxSiz; ++i)
  {
    int nx, nySiz;
    cin >> nx >> nySiz;
    for (int j = 0; j < nySiz; ++j)
    {
      int ny;
      cin >> ny;
      rvvnAdjcy[nx][ny] = 1;
    }
  }
  StDist oDist(nIMPOS);
  rvoDist.resize(nxSiz + 1, oDist);
}

void fnBreadthFirstSearch(const vector<vector<int>>& cnrvvnAdjcy, 
                          queue<int>& rqnBFSrch, vector<StDist>& rvoDist)
{
  while (rqnBFSrch.size())
  {
    int nx = rqnBFSrch.front(); rqnBFSrch.pop();

    for (int ny = 1; ny < cnrvvnAdjcy[nx].size(); ++ny)
    {
      if (!cnrvvnAdjcy[nx][ny])          continue;
      if (rvoDist[ny].m_nDist != nIMPOS) continue; 

      int nDist = rvoDist[nx].m_nDist + 1;
      rvoDist[ny].m_nDist = nDist;
      rqnBFSrch.push(ny);
    }
  }
}

void fnResult(const vector<StDist>& cnrvoDist)
{
  for (int i = 1; i < cnrvoDist.size(); ++i)
    cout << i <<  " " << cnrvoDist[i].m_nDist << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<vector<int>> vvnAdjcy;
  queue<int>  qnBFSrch;
  vector<StDist> voDist;

  fnInput(vvnAdjcy, voDist);

  voDist[1].m_nDist = 0;
  qnBFSrch.push(1);
  fnBreadthFirstSearch(vvnAdjcy, qnBFSrch, voDist);
  fnResult(voDist);

  return 0;
}

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

int t[110];	
vector<int> a[110];

void deep(int n,int time) {
	time++;
	for (int i = 0; i < a[n].size(); i++) {
		if (t[a[n][i]] >time) {
			t[a[n][i]] = time;
			deep(a[n][i],time);
		}
		else if (t[a[n][i]] == -1) {
			t[a[n][i]] = time;
			deep(a[n][i], time);
		}
	}
}


int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	
	int N; cin >> N;
	int u, k, v;
	fill(t, t + 110, -1);
	t[1] = 0;
	for (int i = 0; i < N; i++) {
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v;
			a[u].push_back(v);
		}
	}
	deep(1, 0);
	for (int i = 1; i <= N; i++) {
		cout << i << " " << t[i] << endl;
	}
}