Problem:
Score: 500 points

Problem Statement

Does there exist an undirected graph with N vertices satisfying the following conditions?

The graph is simple and connected.

The vertices are numbered 1, 2, ..., N.

Let M be the number of edges in the graph. The edges are numbered 1, 2, ..., M, the length of each edge is 1, and Edge i connects Vertex u_i and Vertex v_i.

There are exactly K pairs of vertices (i,\ j)\ (i < j) such that the shortest distance between them is 2.

If there exists such a graph, construct an example.

Constraints

All values in input are integers.

2 \leq N \leq 100

0 \leq K \leq \frac{N(N - 1)}{2}

Input

Input is given from Standard Input in the following format:

N K

Output

If there does not exist an undirected graph with N vertices satisfying the conditions, print -1.

If there exists such a graph, print an example in the following format (refer to Problem Statement for what the symbols stand for):

M
u_1 v_1
:
u_M v_M

If there exist multiple graphs satisfying the conditions, any of them will be accepted.

Sample Input 1

5 3

Sample Output 1

5
4 3
1 2
3 1
4 5
2 3

This graph has three pairs of vertices such that the shortest distance between them is 2: (1,\ 4), (2,\ 4), and (3,\ 5). Thus, the condition is satisfied.

Sample Input 2

5 8

Sample Output 2

-1

There is no graph satisfying the conditions.

Solution in C (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>

int main(void){
  int N,K;
  scanf("%d%d",&N,&K);

  int tmp = N-2;
  int res = 0;
  while(tmp > 1){
    res += tmp;
    tmp--;
  }
  if(res < K){
    printf("-1\n");
    return 0;
  }

  int data[102] = {0 ,0 ,0 , 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, 1326, 1378, 1431, 1485, 1540, 1596, 1653, 1711, 1770, 1830, 1891, 1953, 2016, 2080, 2145, 2211, 2278, 2346, 2415, 2485, 2556, 2628, 2701, 2775, 2850, 2926, 3003, 3081, 3160, 3240, 3321, 3403, 3486, 3570, 3655, 3741, 3828, 3916, 4005, 4095, 4186, 4278, 4371, 4465, 4560, 4656, 4753, 4851, 4950};

  int M = 0;
  int hoge[100] = {};
  int cnt = 0;
  for (int i = 101; i >= 0; i--) {
    while(data[i] <= K && K != 0){
      if(K != 0) hoge[cnt++] = i-1;
      K -= data[i];
      /* printf("%d\n",K); */
    }
  }

  int past = 1;
  int num = 0;
  for (int i = 0; i < cnt; i++) {
    num += hoge[i]+1;
    for (int i = past+1; i <= num; i++) {
      M++;
    }
    for (int i = past; i <= num; i++) {
      for (int j = 1; j < past; j++) {
        M++;
      }
      for (int j = num+1; j <= N; j++) {
        M++;
      }
    }
    past = num+1;
  }

  printf("%d\n",M);

  past = 1;
  num = 0;
  for (int i = 0; i < cnt; i++) {
    num += hoge[i]+1;
    for (int i = past+1; i <= num; i++) {
      printf("%d %d\n",past,i);
    }
    for (int i = past; i <= num; i++) {
      for (int j = 1; j < past; j++) {
        printf("%d %d\n",i,j);
      }
      for (int j = num+1; j <= N; j++) {
        printf("%d %d\n",i,j);
      }
    }
    past = num+1;
  }

  /* for (int i = 0; i < cnt; i++) { printf("hoge[%d] = %d\n",i,hoge[i]); */
  /* } */

  return 0;
}