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.*;
import java.io.*;

public class Main {
    public static void main(String[] args) {
        FastScanner scanner = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] degs = new int[N];
        Arrays.fill(degs, 2);
        degs[0] = degs[N-1] = 1;
        int num = N-2;
        boolean moved = true;
        while(moved && num < K) {
            moved = false;
            int ind;
            int lost = 0;
            for(ind = 0; ind < N; ind++) {
                if (degs[ind] == 1) {
                    degs[ind]--;
                    if (ind + 1 < N && degs[ind + 1] > 0) {
                        degs[ind + 1]--;
                        lost = degs[ind + 1];
                    }
                    if (ind - 1 >= 0 && degs[ind -1] > 0) {
                        degs[ind -1] --;
                        lost = degs[ind -1];
                    }
                    break;
                }
            }
            if (ind == N) continue;
            int bestVal = -1;
            int best = -1;
            int nextNum = num - lost;
            for(int i = 0; i < N; i++) {
                int t = nextNum + degs[i];
                if ( t<= K && t > bestVal) {
                    best = i;
                    bestVal = t;
                }
            }
            if (bestVal >= num) {
                moved = true;
                degs[best]++;
                num = bestVal;
            }
        }
        ArrayList<Pair> list = new ArrayList<>();
        if (num != K) out.println(-1);
        else {
            int nextNode = 1;
            int carry = 0;
            for(int i = 0; i < N; i++) {
                int prev = degs[i];
                int cur = nextNode;
                while (degs[i] > carry) {
                    list.add(new Pair(cur, ++nextNode));
                    degs[i]--;
                }
                if (prev > 0) carry = 1;
            }
            out.println(list.size());
            for(Pair edge: list) {
                out.println(edge.a + " " + edge.b);
            }
        }
        out.flush();
    }
    
    static class Pair implements Comparable<Pair> {
        int a, b;
        public Pair(int aa, int bb) {
            a = aa; b = bb;
        }
        public int compareTo(Pair o) {
            if (b == o.b) return a - o.a;
            return b - o.b;
        }
    }
    
    public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        
        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }
        
        public FastScanner() {
            this(new InputStreamReader(System.in));
        }
        
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        
        int nextInt() {
            return Integer.parseInt(next());
        }
        
        long nextLong() {
            return Long.parseLong(next());
        }
        
        double nextDouble() {
            return Double.parseDouble(next());
        }
        
        String readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
        
        int[] readIntArray(int n) {
            int[] a = new int[n];
            for (int idx = 0; idx < n; idx++) {
                a[idx] = nextInt();
            }
            return a;
        }
    }
}