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 <bits/stdc++.h>
       
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
       
       

struct Edge {
  int from, to;
  Edge(){};
  Edge(int from, int to) : from(from), to(to) {}

  void scanFrom(std::istream& strm, int base = 1) {
    strm >> from >> to;
    if (base == 1) --from, --to;
  }

  Edge reverse() const { return Edge(to, from); }
};

inline std::istream& operator>>(std::istream& strm, Edge& t) {
  t.scanFrom(strm);
  return strm;
}
       
       

template <typename T>
struct WeightedEdge {
  int from, to;
  T _weight;
  WeightedEdge(){};
  WeightedEdge(int from, int to, T weight)
      : from(from), to(to), _weight(weight) {}

  void scanFrom(std::istream& strm, int base = 1) {
    strm >> from >> to >> _weight;
    if (base == 1) --from, --to;
  }

  T weight() const { return _weight; }

  WeightedEdge reverse() const { return WeightedEdge(to, from, _weight); }
};

template <typename T>
inline std::istream& operator>>(std::istream& strm, WeightedEdge<T>& t) {
  t.scanFrom(strm);
  return strm;
}

template <typename Edge>
class Digraph {
  int _E, _V;
  vector<vector<Edge>> _adjacencyList;

 public:
  int E() const { return _E; }

  int V() const { return _V; }
  int size() const { return _V; }

  Digraph() : _V(0), _E(0){};
  Digraph(int V, int E = 0) : _V(V), _E(E) { _adjacencyList.resize(V); }

  void addEdge(const Edge& e) {
    _adjacencyList[e.from].push_back(e);
    ++_E;
  }

  void addUndirectedEdge(const Edge& e) {
    _adjacencyList[e.from].push_back(e);
    _adjacencyList[e.to].push_back(e.reverse());
    ++_E;
  }

  void addNode() {
    ++_V;
    _adjacencyList.push_back(vector<Edge>());
  }

  void scanFrom(std::istream& strm, int base = 1) {
    if (_V == 0) strm >> _V;
    if (_E == 0) strm >> _E;
    _adjacencyList.resize(_V);
    for (int i = 0; i < _E; ++i) {
      Edge e;
      e.scanFrom(cin, base);
      assert(0 <= e.from < _V);
      assert(0 <= e.to < _V);
      addEdge(e);
    }
  }

  vector<Edge> operator[](int u) const { return _adjacencyList[u]; }
  vector<vector<Edge>>& adjacencyList() { return _adjacencyList; }
};

template <typename Edge>
inline std::istream& operator>>(std::istream& strm, Digraph<Edge>& t) {
  t.scanFrom(strm);
  return strm;
}

class BFSShortestPath {
  const int INF = numeric_limits<int>::max();
  vector<int> _distTo;
  vector<int> prev;

 public:
  BFSShortestPath(const Digraph<Edge>& G, int s)
      : _distTo(G.V(), INF), prev(G.V(), -1) {
    queue<int> q;
    q.push(s);
    _distTo[s] = 0;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (Edge& e : G[u]) {
        if (_distTo[e.to] != INF) continue;
        _distTo[e.to] = _distTo[u] + 1;
        prev[e.to] = u;
        q.push(e.to);
      }
    }
  }

  int distTo(int u) { return _distTo[u]; }

  bool hasPathTo(int u) { return _distTo[u] != INF; }

  vector<int> pathTo(int u) {
    assert(hasPathTo(u));
    vector<int> path;
    while (u != -1) {
      path.push_back(u);
      u = prev[u];
    }
    return path;
  }
};

int main() {
  int n;
  cin >> n;
  Digraph<Edge> G(n);

  for (int i = 0; i < (int)(n); ++i) {
    int u, k;
    cin >> u >> k;
    --u;
    for (int j = 0; j < (int)(k); ++j) {
      int v;
      cin >> v;
      --v;
      G.addEdge(Edge(u, v));
    }
  }

  BFSShortestPath sp(G, 0);
  for (int i = 0; i < (int)(n); ++i) {
    cout << i + 1 << " " << (sp.hasPathTo(i) ? sp.distTo(i) : -1) << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <queue>
using namespace	std;
int main(){

  int n,g[100][100]={},tmp1,amo,tmp2;
  queue <int> q;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>tmp1>>amo;
    for(int j=0;j<amo;j++){
      cin>>tmp2;
      g[tmp1-1][tmp2-1]=1;
    }
  }
  int dis[100]={},flag[100]={1};
  for(int i=0;i<n;i++) if(i) dis[i]=-1;
  q.push(0);
  for(;!q.empty();){
    for(int i=0;i<n;i++) if(g[q.front()][i]&&flag[i]!=1){
        dis[i]=dis[q.front()]+1;
        flag[i]=1;
        q.push(i);
      }
    q.pop();
  }
  for(int i=0;i<n;i++) cout<<i+1<<" "<<dis[i]<<endl;
  return 0;

}

C++: #include "bits/stdc++.h"

using namespace std;

void Main() {
    int n;
    cin >> n;
    vector<set<int>> graph(n, set<int>());
    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;
            graph[u].insert(v);
        }
    }

    queue<pair<int, int>> q;
    vector<int> distances(n, -1);
    q.push(make_pair(0, 0));
    distances[0] = 0;
    while (!q.empty()) {
        pair<int, int> curr_dist = q.front();
        q.pop();
        int curr = curr_dist.first;
        int dist = curr_dist.second;
        const set<int>& nexts = graph.at(curr);
        for (int e : nexts) {
            if (distances[e] < 0) {
                q.push(make_pair(e, dist + 1));
                distances[e] = dist + 1;
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        cout << i + 1 << " " << distances[i] << endl;
    }
}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}

C++: #include<iostream>
#include<queue>
#define black 2
#define white 0
#define gray 1
using namespace std;
int matrix[101][101];
int n;
int temp;
int d[101]={-1};
int color[101]={white};
int main(){
    for(int i=0;i<101;i++){
        for(int j=0;j<101;j++)
            matrix[i][j]=0;
    }
    cin>>n;
    int x,y;
    for(int i=0;i<n;i++){
        cin>>x>>temp;
        for(int j=0;j<temp;j++){
            cin>>y;
            matrix[x][y]=1;
        }
    }
    queue <int> q;
    q.push(1);
    for(int i=0;i<=n;i++)
      d[i]=-1;
    color[1]=gray;
    d[1]=0;

    while(!q.empty()){
        temp=q.front();
        for(int i=1;i<=n;i++){
            if(matrix[temp][i]==1&&color[i]==white){
                q.push(i);
                color[i]=gray;
                d[i]=d[temp]+1;
            }
        }
        color[temp]=black;
        q.pop();
    }
    for(int i=1;i<=n;i++)
        cout<<i<<" "<<d[i]<<endl;
    return 0;
}