Go: package main

import (
	"container/list"
	"fmt"
	"bufio"
	"os"
	"strconv"
)

const (
	N   = 100
	INF = 1 << 21
)

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(0)
}

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

vector<vector<int> > vec;
vector<int> ve;
int dis[MAX];
queue<int> Q;
int n;

int search(int start, int end) {
	while(!Q.empty()) Q.pop();
	Q.push(start);
	for (int i = 1; i <= n; i++) {
		dis[i] = 0;
	}
	int now, len = 0;
	while (!Q.empty()) {
		now = Q.front();
		Q.pop();
		if (now == end)
			return dis[now];
		if (dis[now] == len)
			len++;
		for (int cnt = 1; cnt <= vec[now][0]; cnt++) {
			if (dis[vec[now][cnt]] == 0) {
				dis[vec[now][cnt]] = len;
				Q.push(vec[now][cnt]);
			}
		}
	}
	return -1;
}

int main(void) {
	vec.push_back(ve);
	int u, k, v, i;
	cin >> n;
	for (i = 1; i <= n; i++) {
		ve.clear();
		cin >> u >> k;
		ve.push_back(k);
		while (k--) {
			cin >> v;
			ve.push_back(v);
		}
		vec.push_back(ve);
	}

	for (i = 1; i <= n; i++) {
		cout << i << " " << search(1, i) << endl;
	}
	
}

C++: #include<iostream>
#include<queue>
using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);

int n, M[N][N], 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 v, k ,u;
	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;
		for(int j = 0; j < k; j++) {
			cin >> v;
			M[u-1][v-1] = 1;
		}
	}
	
	bfs(0);
	
	return 0;
	
}

C++: #include <iostream>
#include <stack>
#include <queue>

using namespace std;

#define MAX 100

#define WHITE 0
#define GRAY 1
#define BLACK 2

int T;
int N;

int M[MAX][MAX];
int C[MAX],D[MAX];

void bfs(int s)
{
    queue<int> *q = new queue<int>;

    q->push(s);
    D[s] = 0;
    C[s] = GRAY;

    while(q->size() > 0)
    {
        int u = q->front();
        q->pop();

        for (int i = 0; i < N; i++)
        {
            if (M[u][i] && C[i] == WHITE)
            {
                C[u] = GRAY;
                D[i] = D[u] + 1;
                q->push(i);
            }
        }

        C[u] = BLACK;
    }
}

int main ()
{
    cin >> N;

    for (int i = 0; i < N; i++)
    {
        C[i] = WHITE;
        D[i] = -1;

        for (int j = 0; j < N; j++)
        {
            M[i][j] = 0;
        }
    }

    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--;
            M[u][v] = 1;
        }
    }

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (j > 0)
                cout << " ";
            cout << M[i][j];
        }
        cout << endl;
    }

    bfs(0);

    for (int i = 0; i < N; i++)
    {
        cout << D[i] << endl;
    }

    return 0;
}

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

typedef vector<int> Adj_list;
typedef vector<Adj_list> Adj_lists;

void bfs(const int& i, const Adj_lists& adj_lists, vector<int>& distances){

  distances.at(i-1) = 0;

  queue<int> q;
  q.push(i);

  while(!q.empty()){
    // visit top of queue
    const int& visit = q.front();
    const Adj_list& adj_list = adj_lists.at(visit-1);

    // push adjecent node which is not visited yet
    for(const int& adj : adj_list){
      if(distances.at(adj-1) == -1){
        distances.at(adj-1) = distances.at(visit-1) + 1;
        q.push(adj);
      }
    }

    q.pop();
  }
}

int main(){

  int n;
  scanf("%d", &n);

  Adj_lists adj_lists(n);
  for(int i=0; i<n; i++){
    int u, k;
    scanf("%d %d", &u, &k);
    Adj_list adj_list(k);
    for(int j=0; j<k; j++){
      int v;
      scanf("%d", &v);
      adj_list[j] = v;
    }
    sort(adj_list.begin(), adj_list.end());
    adj_lists[u-1] = adj_list;
  }

  vector<int> distances(n);
  for(int& distance : distances) distance = -1;

  bfs(1, adj_lists, distances);

  for(int i = 0; i < distances.size(); i++){
    printf("%d %d\n", i+1, distances.at(i));
  }

  return 0;
}

C++: #include <cstdint>
#include <queue>
#include <vector>

using namespace std;

#define GRAPH_SIZE 101
#define START_INDEX 1

enum color_t {
  WHITE,
  GRAY,
  BLACK,
};

struct data_t {
  uint32_t index;
  vector<uint32_t> connection;
  uint32_t distance;
};

static vector<uint32_t> graph[GRAPH_SIZE];
static color_t state[GRAPH_SIZE];

void breadth_first_search(void (*callback)(uint32_t, uint32_t))
{
  for (uint32_t i = START_INDEX; i < GRAPH_SIZE; i++) {
    state[i] = WHITE;
  }

  queue<data_t> queue;

  data_t data;
  data.index = START_INDEX;
  data.connection = graph[START_INDEX];
  data.distance = 0;

  queue.push(data);
  state[START_INDEX] = GRAY;

  while (!queue.empty()) {
    data_t top = queue.front();
    queue.pop();

    state[top.index] = BLACK;
    callback(top.index, top.distance);

    for (uint32_t i = 0; i < top.connection.size(); i++) {
      if (state[top.connection[i]] == WHITE) {
        data_t data;
        data.index = top.connection[i];
        data.connection = graph[top.connection[i]];
        data.distance = top.distance + 1;
        queue.push(data);
        state[data.index] = GRAY;
      }
    }
  }
}

#include <iostream>

int32_t distances[GRAPH_SIZE];

static void callback(uint32_t index, uint32_t distance)
{
  distances[index] = distance;
}

int32_t main()
{
  uint32_t num;
  cin >> num;

  for (uint32_t i = START_INDEX; i < START_INDEX + num; i++) {
    distances[i] = -1;
  }

  for (uint32_t i = 0; i < num; i++) {
    uint32_t index, len;
    cin >> index >> len;
    for (uint32_t j = 0; j < len; j++) {
      uint32_t val;
      cin >> val;
      graph[index].push_back(val);
    }
  }

  breadth_first_search(callback);

  for (uint32_t i = START_INDEX; i < START_INDEX + num; i++) {
    cout << i << " " << distances[i] << endl;
  }

  return 0;
}