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.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.math.BigInteger;
 
public class Main implements Runnable {
	
	static int mod = 1000000007;
	
    public static void main(String[] args) {
    	new Thread(null, new Main(), "", 1024 * 1024 * 1024).start();
    }
    
    public void run() {
    	PrintWriter out = new PrintWriter(System.out);
        FastScanner sc = new FastScanner();
        
        int n = sc.nextInt();
        int k = sc.nextInt();
        
        Combinatrics c = new Combinatrics(n);
        
        int p = n/2;
        int q =	n-p;
        
        int pmax = c.combination(p, 2);
        int qmax = c.combination(q, 2);
        
        if(pmax + qmax < k){
        	out.println(-1);
        }
        else{
        	for(int i=1;i<=p;i++){
        		for(int j=p+1;j<=n;j++){
            		out.println(i + " " + j);
        		}
        	}
        	int delete = pmax + qmax - k;
        	int pdelete = Math.min(pmax, delete);
        	int qdelete = Math.max(0, delete - pdelete);
        	
    		int nowmae = 1;
    		int nowusi = 2;
    		while(pdelete>0){
    			pdelete--;
    			out.println(nowmae + " " + nowusi);
    			if(nowusi == p){
    				nowmae ++;
    				nowusi = nowmae+1;
    			}
    			else{
    				nowusi++;
    			}
    		}
    		nowmae = p+1;
    		nowusi = p+2;
    		while(qdelete>0){
    			qdelete--;
    			out.println(nowmae + " " + nowusi);
    			if(nowusi == n){
    				nowmae ++;
    				nowusi = nowmae+1;
    			}
    			else{
    				nowusi++;
    			}
    		}
        	
        }
        
        out.flush();
    }
    

}

class Combinatrics {
	
	static long mod = 1000000007; //逆元を取るので素数前提
	static long[] fact; //階乗のテーブル
	static long[] inv;	//逆元のテーブル
	static long[] finv; //階乗の逆元のテーブル
	
	public Combinatrics(int n){
		int N = Math.max(2,n+1);
		fact = new long[N];
		inv = new long[N];
		finv = new long[N];
	    fact[0] = fact[1] = 1;
	    finv[0] = finv[1] = 1;
	    inv[1] = 1;
	    for(int i=2;i<N; i++){
	        fact[i] = fact[i-1] * i % mod;
	        inv[i] = mod - inv[(int)mod%i] * (mod / i) % mod;
	        finv[i] = finv[i-1] * inv[i] % mod;
	    }
	}
	
	//逆元
	public int inv(int n){
		return (int)inv[n];
	}
	
	//n!
	public int factorial(int n){
		return (int)fact[n];
	}
	
	//n!の逆元
	public int factInv(int n){
		return (int)finv[n];
	}
	
	//nCr（n個からr個選ぶ）
    public int combination(int n, int r) {
        if(n<r|| r<0){
        	return 0;
        }
        return (int)(fact[n]*finv[r]%mod*finv[n-r]%mod);
    }

    //nPr (=n*(n-1)*...*(n-r+1))（n個からr個選んで並べる）
    public int permutation(int n, int r) {
        if(n<r || r<0){
        	return 0;
        }
        return (int)(fact[n]*finv[n-r]%mod);
    }

    //nHr (=n+r-1Cr)（n個から重複を許してk個とる）
    //（テーブルがn+r-1まで必要な事に注意！）
    public int homogeneous(int n, int r) {
        if(n==0 && r==0){
        	return 1;
        }
        return combination(n+r-1,r);
    }
    
    //全射の個数（テーブルはkまで必要）
    //n個の区別可能なボールをちょうどk個の区別可能な箱に入れる組合せの数
    public int surjection(int n, int k){
    	long ans = 0;
    	int sign = 1; //符号
    	if(k%2 == 0){
    		sign = -1;
    	}
    	//包除原理
    	for(int i=1;i<=k;i++){
    		if(sign==1){
    			ans += (long)combination(k,i)*iterationPow(i,n);
    			ans %= mod;
    		}
    		else{
    			ans -= (long)combination(k,i)*iterationPow(i,n);
    			ans %= mod;
    		}
    		sign *= -1;
    	}
    	ans = mod(ans);
    	return (int)ans;
    }
	//x^nをmodのもとで繰り返し2乗法で求める。
	static long iterationPow(long x, long n){
	    long sum = 1;
	    while(n>0){
	        if((n&1)== 1){  //ビットが立っていたら
	            sum *= x;        //x^2^k1 * x^2^k2 * x^2^k3 * ・・・
	            sum %= mod;
	        }
	        x *= x;              //((x^2)^2)^2・・・
	        x %= mod;
	        n >>= 1;
	    }
	    return sum;
	}
	//負の数に対応したmod
	static long mod(long a) {
		return (a % mod + mod) % mod;
	}
    
	//スターリング数（テーブルはkまで必要）
	//n個の区別可能なボールをちょうどk個の区別不可能な箱に入れる組合せの数
	public int stirling(int n, int k){
		//全射の個数をk!で割ったものに等しい
		return (int) ((long)surjection(n,k)*(long)factInv(k) % mod);
	}
	
	//ベル数（テーブルはkまで必要）
	//n個の区別可能なボールをk個以下の区別不可能な箱に入れる組合せの数
	public int bell(int n, int k){
		int ans = 0;
		//S(n,1)からS(n,k)まで足し合わせる
		for(int i=1;i<=k;i++){
			ans += stirling(n,i);
			ans %= mod;
		}
		return ans;
	}
	
}

class FastScanner {
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;
	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		} else {
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
		}
		return true;
	}
	private int readByte() {
		if (hasNextByte())
			return buffer[ptr++];
		else
			return -1;
	}
	private static boolean isPrintableChar(int c) {
		return 33 <= c && c <= 126;
	}
	public boolean hasNext() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr]))
			ptr++;
		return hasNextByte();
	}
	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while (isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	public long nextLong() {
		if (!hasNext())
			throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while (true) {
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isPrintableChar(b)) {
				return minus ? -n : n;
			} else {
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}
	public int nextInt() {
		long nl = nextLong();
		if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
			throw new NumberFormatException();
		return (int) nl;
	}
	public int[] nextintArray(int n){
		int[] a = new int[n];
		for(int i=0;i<n;i++){
			a[i] = nextInt();
		}
		return a;
	}
	public long[] nextlongArray(int n){
		long[] a = new long[n];
		for(int i=0;i<n;i++){
			a[i] = nextLong();
		}
		return a;
	}
	public Integer[] nextIntegerArray(int n){
		Integer[] a = new Integer[n];
		for(int i=0;i<n;i++){
			a[i] = nextInt();
		}
		return a;
	}
	public double nextDouble() {
		return Double.parseDouble(next());
	}
}