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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.Scanner;
import java.awt.Point;
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n =sc.nextInt();
		int k=sc.nextInt();
		int[][] map=new int[n][n];
		for(int i = 0; i<n; i++){
			for(int j = 0; j<n; j++){
				if(i!=j){
					map[i][j]=Integer.MAX_VALUE/10;
				}
			}
		}
		for(int i=0; i<n-1; i++){
			map[i][i+1]=1;
			map[i+1][i]=1;
		}
		for(int i = 0; i<n; i++){
			for(int j = 0; j<n; j++){
				for(int t = 0; t<n; t++){
						map[j][t]=Math.min(map[j][t],map[j][i]+map[i][t]);
				}
			}
		}
		int count=0;
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				if(map[i][j]==2){
					count++;
				}
			}
		}
		count/=2;
		if(count<k){
			System.out.println(-1);
			return;
		}else if(count==k){
			System.out.println(n-1);
			for(int i=0; i<n-1; i++){
				System.out.println((i+1)+" "+(i+2));
			}
			return;
		}
		int cc=count-k;
		int m=cc+n-1;
		Point[] qq=new Point[cc];
		cc--;
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				if(map[i][j]==2){
					map[j][i]=0;
					qq[cc--]=new Point(i+1,j+1);
					if(cc<0){
						System.out.println(m);
						for(int u=0; u<n-1; u++){
							System.out.println((u+1)+" "+(u+2));
						}
						for(int u=0; u<qq.length; u++){
							System.out.println(qq[u].x+" "+qq[u].y);
						}
						return;
					}
				}
			}
		}
	}
}