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 (Python):
n,k=map(int,input().split())
d=[[i+1,j+1]for i in range(n)for j in range(i+1,n)][::-1][k:]*(k<=(n-2)*~-n//2)
print(len(d)or-1,*sum(d,[]))

Solution (Python):
N, K = map(int,input().split())
t = ((N-1)*(N-2))//2

if t < K:
    print(-1)
    exit(0)

A = []
for k in range(2,N+1):
    A.append([1,k])

f = 0
for k in range(2,N):
    for l in range(k+1,N+1):
        if f == t-K:
            print(N-1+f)
            for e in A:
                print(*e)
            exit(0)
        else:
            A.append([k,l])
            f += 1
print(N-1+f)
for e in A:
    print(*e)

Solution (Python):
N,K = [int(x) for x in input().split()]
x = (N-1)*(N-2)//2
edge = 2
cnt = 0
li = []
if(x<K):
    print(-1)
else:
    for i in range(2,N+1):
        li.append([1,i])
    
    for i in range(N-2,0,-1):
        for j in range(i):
            if(cnt>=x-K):
                break
            li.append([N-i,N-i+j+1])
            cnt+=1
            if(cnt>=x-K):
                break
        else:
            continue
        break
    if(N==3 and K==1):
        li.pop()
    if(N==4 and K==3):
        li.pop()
    print(len(li))
    for i in li:
        print(i[0],i[1])

Solution (Python):
from collections import Counter
from collections import deque
from functools import reduce
from pprint import pprint
import bisect
import copy
import fractions
import itertools
import math
import queue
import random
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def C(x): return Counter(x)
def GCD_LIST(numbers): return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers): return reduce(LCM, numbers)
def LCM(m, n): return (m * n // fractions.gcd(m, n))


n, k = MI()
if ((n - 1) * (n - 2)) // 2 < k:
    print(-1)
else:
    m = (n * (n - 1)) // 2 - k
    print(m)
    graph = []
    for i in range(1, n + 1):
        for j in range(i, n + 1):
            if i != j:
                graph.append([i, j])
    for i in range(m):
        print(*graph[i])

Solution (Python):
n,k = map(int, input().split())

cur = (n - 1) * (n - 2) // 2
if k > cur:
    print(-1)
    exit()


res = []
for i in range(2, n + 1):
    res.append([1, i])

l, r = 2, 3
while cur > k:
    cur -= 1
    res.append([l, r])
    r += 1
    if r > n:
        l += 1
        r = l + 1

print(len(res))
for i in res:
    print(*i)

Solution (Python):
n,k = map(int,input().split())
s= n
ans=[]
freeNode = [2]
cur=2
ans.append([1,2])
if n==2 :
    if k>0:
        print(-1)
    else:
        print(1)
        print(1, 2)
        exit()
else:
    s=0
    c=1
    if (n-2)*(n-1)//2<k:
        print(-1)
        exit()
    else:
        while(1):
            s += c
            cur+=1
            c+=1
            freeNode.append(cur)
            ans.append([1,cur])
            if s >=k:
                break
    if cur!=n:
        remove = s-k;
        if remove:
            for v in freeNode:
                ans.append([v,cur])
                remove-=1
                if remove==0:
                    break
        for i in range(cur + 1, n + 1):
            cur+=1
            for j in range(1, cur):
                ans.append([i, j])
        print(len(ans))
        for u,v in ans:
            print(u,v)
    else:
        remove = s - k;
        if remove:
            for v in freeNode:
                ans.append([v, cur])
                remove -= 1
                if remove == 0:
                    break
        print(len(ans))
        for u, v in ans:
            print(u, v)

Solution (Python):
N, K = map(int, input().split())

def method(number, remain):
    if (remain * (remain+1)) // 2 < number:
        return 0, 0
    i = 1
    remain -= 1
    sumple = (i * (i + 1)) // 2
    while sumple + remain < number:
        i += 1
        sumple = (i * (i + 1)) // 2
        remain -= 1
    return i, remain 

i, remain = method(K, N-2)
if i > 0:
    sub = ((i+1)*i)//2 + remain - K
    if i > 1:
        M = (N - 1) + sub
    else:
        M = (N - 1) + sub*(N-2 + N-2-sub+1) // 2
    print(M)
    if i > 1:
        for x in range(i+1):
            print(1, x+2)
        for x in range(i+2, N):
            print(x, x+1)
        for x in range(sub):
            print(2, x+3)
    else:
        for x in range(N-1):
            print(x+1, x+2)
        for x in range(sub):
            for y in range(x,N-2):
                print(x+1, y+3)
else:
    print(-1)

Solution (Python):
n,k = map(int,input().split())
count = 0
c = int((n-1)*(n-2)/2)
if k > c:
    print(-1)
else:
    print(n-1+c-k)
    for i in range(n-1):
        print(1,i+2)
    if k < c:
        for i in range(2,n+1):
            for j in range(i+1,n+1):
                print(i,j)
                count = count + 1
                if count == c-k:
                    break
            if count == c-k:
                break

Solution (Python):
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque

from itertools import product

from itertools import permutations

from itertools import combinations

from itertools import accumulate
from bisect import bisect_left, bisect_right





def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")



N, K = l_inpl()

max_K = (N-1)*(N-2)//2
if K>max_K:
    print("-1")
else:
    ans = [[1, x] for x in range(2, N+1)]
    cnt = max_K - K
    c = list(combinations(list(range(2, N+1)), 2))
    for i in range(cnt):
        ans.append(list(c[i]))
    print(len(ans))
    for a in ans:
        print("{} {}".format(a[0], a[1]))

Solution (Python):
n, k = map(int, input().split())

if k > (n - 1) * (n - 2) // 2:
    print(-1)
    exit()

ans = [[0 for _ in range(n)] for _ in range(n)]

for i in range(1, n):
    ans[0][i] = 1

m = n-1

temp = (n-1) * (n-2) // 2 - k
for i in range(1, n):
    if temp == 0:
        break
    for j in range(i+1, n):
        ans[i][j] = 1
        m += 1
        temp -= 1
        if temp == 0:
            break

print(m)
for i in range(n):
    for j in range(n):
        if ans[i][j] == 1:
            print(i+1, j+1)

Solution (Python):
n, k = map(int, input().split())

k_max = (n-1)*(n-2)//2
if k > k_max:
	print(-1)
	exit()

m = (n-1) + k_max - k
print(m)

for i in range(n-1):
	print(1, i+2)
ptr1 = 2
ptr2 = 3
for _ in range(k_max - k):
	print(ptr1, ptr2)
	if ptr2 < n:
		ptr2 += 1
	else:
		ptr1 += 1
		ptr2 = ptr1 + 1

Solution (Python):
N, K = map(int, input().split())
ans = []
if ((N - 1) * (N - 2)) / 2 < K:
    print('-1')
else:
    M = (N - 1) * (N - 2) // 2
    for i in range(N - 1):
        ans.append([1, i + 2])
    j, k = 2, 3
    while (M > K):
        ans.append([j, k])
        M -= 1
        k += 1
        if k > N:
            j += 1
            k = j + 1
    print(len(ans))
    for out in ans:
        print(*out)

Solution (Python):
n,k=map(int,input().split())
if k>((n-2)*(n-1))//2:
  print(-1)
else:
  cnt=0
  print(-k+((n-2)*(n-1))//2+n-1)
  for i in range(2,n+1):
    print(1,i)
    cnt+=1
  for i in range(2,n):
    for j in range(i+1,n+1):
      if cnt==-k+((n-2)*(n-1))//2+n-1:
        break
      print(i,j)
      cnt+=1

Solution (Python):
import itertools

N, K = list(map(int, input().split()))







if K > (N - 1) * (N - 2) // 2:
    print(-1)





else:
    diff = (N - 1) * (N - 2) // 2 - K
    print(N - 1 + diff)
    
    for i in range(N - 1):
        print(' '.join(map(str, [1, i + 2])))

    lst = list(range(2, N+1))
    combi = list(itertools.combinations(lst, 2))
    for i in range(diff):
        print(' '.join(map(str, combi[i])))

Solution (Python):
n, k = map(int, input().split())
mink = (n - 1) * (n - 2) // 2
if k > mink:
    print(-1)
else:
    m = n - 1 + (mink - k)
    print(m)
    count = 0
    i = 1
    j = 2
    while count < m:
        if j == n + 1:
            i += 1
            j = i + 1
        print(i, j)
        j += 1
        count += 1

Solution (Python):
n, k = map(int, input().split())
mink = (n - 1) * (n - 2) // 2
if k > mink:
    print(-1)
else:
    m = n - 1 + (mink - k)
    print(m)
    count = 0
    i = 1
    j = 2
    while count < m:
        if j == n + 1:
            i += 1
            j = i + 1
        print(i, j)
        j += 1
        count += 1

Solution (Python):
input = input().split(' ')
N = int(input[0])
K = int(input[1])

total = 0
edges = []
for i in range(2, N+1):
    edges.append([1, i])
    total += N-i
if total < K:
    print(-1)
else:
    for i in range(2, N):
        for j in range(i+1, N+1):
            if total == K:
                break
            total -= 1
            edges.append([i, j])
        if total == K:
            break

    print(len(edges))
    for e in edges:
        print('{} {}'.format(e[0], e[1]))

Solution (Python):
from itertools import combinations as comb

def main():
    N,K = map(int,input().split())
    
    Max = (N-1)*(N-2)//2  
    if K>Max:
        print(-1)
    else:
        M = Max - K
        Edge = [(1,_) for _ in range(2, N+1)]
        Edge += list(comb(range(2,N+1),2))[:M]
        print(len(Edge))
        for i,j in Edge:
            print(i,j)
            
if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
K0 = (N - 1) * (N - 2) // 2
if K > K0:
	print(-1)
else:
	print((N - 1) + (K0 - K))  
	
	for i in range(2, N + 1):
		print(1, i)
	
	cnt = 0
	for i in range(2, N):
		for j in range(i + 1, N + 1):
			cnt += 1
			if cnt > K0 - K:
				break
			print(i, j)

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    N, K = map(int, input().split())

    if K > (N-1)*(N-2)//2:
        print(-1)
    else:
        edges = []
        m = N-1+(N-1)*(N-2)//2-K
        for i in range(2, N+1):
            edges.append("{} {}".format(1, i))

        add_edge = (N-1)*(N-2)//2-K

        fr = 2
        to = 3
        while add_edge:
            edges.append("{} {}".format(fr, to))
            to += 1
            if to > N:
                fr += 1
                to = fr+1
            add_edge -= 1
        print(m)
        print("\n".join(edges))


solve()

Solution (Python):
N, K = map(int, input().split())
K0 = (N - 1) * (N - 2) // 2
if K > K0:
	print(-1)
else:
	print((N - 1) + (K0 - K))  
	
	for i in range(2, N + 1):
		print(1, i)
	
	cnt = 0
	for i in range(2, N):
		for j in range(i + 1, N + 1):
			cnt += 1
			if cnt > K0 - K:
				break
			print(i, j)

Solution (Python):

from itertools import combinations




N, K = map(int,input().split())
combi_list = list(combinations([i+1 for i in range(N-1)],2))
r = (N-1)*(N-2) // 2
if (N-1)*(N-2)//2 < K:
    print(-1)
    quit()
else:
    print(N-1 + r - K)
    for i in range(N-1):
        print(N,i+1)
    for i in range(r-K):
        print(combi_list[i][0],combi_list[i][1])

Solution (Python):



N, K = map(int, input().split(" "))

max_edge_count = (N-1)*(N-2)//2
if max_edge_count < K:
    print(-1)
    exit()

edges = []

for i in range(2, N+1):
    edges.append((1, i))

count = max_edge_count - K
for i in range(2, N+1):
    for j in range(i+1, N+1):
        if count == 0:
            print(len(edges))
            for edge in edges:
                print(*edge)
            exit()
        edges.append((i, j))
        count -= 1

print(len(edges))
for edge in edges:
    print(*edge)

Solution (Python):
N, K = map(int, input().split())

con= (N - 1) * (N - 2) // 2 - K
if con < 0:
    print(-1)
else:
    M = N - 1 + con
    print(M)
    for i in range(2, N+1):
        print(1, i)
    cnt = 0
    for i in range(2, N):
        for j in range(i + 1, N + 1):
            cnt += 1
            if cnt > con:
                break
            print(i, j)
        if cnt > con:
            break

Solution (Python):
def solve(N, K):
    M = (N - 1) * (N - 2) // 2 - K
    if M < 0:
        return '-1'

    pairs = []

    for i in range(2, N + 1):
        pairs.append(('1', str(i)))

    m = M
    for i in range(2, N):
        if m == 0:
            break;
        for j in range(i + 1, N + 1):
            if m == 0:
                break;
            m -= 1
            pairs.append((str(i), str(j)))

    return str(len(pairs)) + '\n' + '\n'.join([' '.join(pair) for pair in pairs])


if __name__ == '__main__':
    N, K = map(int, input().split())
    print(solve(N, K))

Solution (Python):
from sys import stdin

def printGraph(hen, graphList):
  print(hen)
  for (i,j) in graphList:
    print("{} {}".format(i, j))

N,K = map(int,stdin.readline().rstrip().split())

maxNum = (N-1)*(N-2)/2
if K > maxNum:
  print("-1")
  exit()


graphList=[]
for i in range(1,N,1):
  graphList.append((i,N))

counter=0
hen=N-1
for i in range(1,N,1):
  for j in range(1,N,1):
    if counter == maxNum - K:
      printGraph(hen,graphList)
      exit()       
    if i < j:
      graphList.append((i,j))
      counter+=1
      hen+=1

Solution (Python):
N, K = tuple(map(int, input().split()))

def make_link(M, s):
    for i in range(2, N+1):
        for j in range(i+1, N+1):
            link[i].append(j)
            M += 1
            s -= 1
            if s == K:
                return M

max_link = (N - 1) * (N - 2) / 2
if K > max_link:
    print(-1)
else:
    link = {i: [] for i in range(1, N+1)}
    for i in range(2, N+1):
        link[1].append(i)
    M = N - 1
    if max_link == K:
        pass
    else:
        M = make_link(M, max_link)
    print(M)
    for i in range(1, N+1):
        for j in link[i]:
            print(i, j)

Solution (Python):







import sys
sys.setrecursionlimit(10**7)
from itertools import accumulate, combinations, permutations 
from math import factorial
def factorize(n):
    """return the factors of the Arg and count of each factor
    
    Args:
        n (long): number to be resolved into factors
    
    Returns:
        list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
    """
    fct = []  
    b, e = 2, 0  
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct
def combinations_count(n, r):
    """Return the number of selecting r pieces of items from n kinds of items.
    
    Args:
        n (long): number
        r (long): number
    
    Raises:
        Exception: not defined when n or r is negative
    
    Returns:
        long: number
    """
    
    if n < 0 or r < 0:
        raise Exception('combinations_count(n, r) not defined when n or r is negative')
    if n - r < r: r = n - r
    if r < 0: return 0
    if r == 0: return 1
    if r == 1: return n
    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]
    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])
    return result
def combinations_with_replacement_count(n, r):
    """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
    
    Args:
        n (long): number
        r (long): number
    
    Raises:
        Exception: not defined when n or r is negative
    
    Returns:
        long: number
    """
    if n < 0 or r < 0:
        raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
    elif n == 0:
        return 1
    else:
        return combinations_count(n + r - 1, r)
from collections import deque, Counter, defaultdict 
from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
from copy import deepcopy, copy 
from operator import itemgetter 


from functools import reduce
from fractions import gcd 
def gcds(numbers):
    return reduce(gcd, numbers)
def lcm(x, y):
    return (x * y) // gcd(x, y)
def lcms(numbers):
    return reduce(lcm, numbers, 1)



INF = 10 ** 18
MOD = 10 ** 9 + 7
modpow = lambda a, n, p = MOD: pow(a, n, p) 
def modinv(a, p = MOD):
    
    
    return modpow(a, p-2, p)
def modinv_list(n, p = MOD):
    if n <= 1:
        return [0,1][:n+1]
    else:
        inv_t = [0,1]
        for i in range(2, n+1):
            inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
        return inv_t
def modfactorial_list(n, p = MOD):
    if n == 0:
        return [1]
    else:
        l = [0] * (n+1)
        tmp = 1
        for i in range(1, n+1):
            tmp = tmp * i % p
            l[i] = tmp
        return l
def modcomb(n, k, fac_list = [], p = MOD):
    
    
    from math import factorial
    if n < 0 or k < 0 or n < k: return 0
    if n == 0 or k == 0: return 1
    if len(fac_list) <= n:
        a = factorial(n) % p
        b = factorial(k) % p
        c = factorial(n-k) % p
    else:
        a = fac_list[n]
        b = fac_list[k]
        c = fac_list[n-k]
    return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
def modadd(a, b, p = MOD):
    return (a + b) % MOD
def modsub(a, b, p = MOD):
    return (a - b) % p
def modmul(a, b, p = MOD):
    return ((a % p) * (b % p)) % p
def moddiv(a, b, p = MOD):
    return modmul(a, modpow(b, p-2, p))



    
    

    
    
    
    
    

    
    

    
    


r = lambda: sys.stdin.readline().strip()
r_int = lambda: int(r())
R = lambda: list(map(int, r().split()))
Rfloat = lambda: list(map(float, r().split()))
Rtuple = lambda: tuple(map(int, r().split()))
Rmap = lambda: map(int, r().split())









N, K = R()
if N == 2:
    if K == 0:
        print(1)
        print(1, 2)
        exit()
    else:
        print(-1)
        exit()
T = combinations_count(N-1, 2)
if K > T:
    print(-1)
    exit() 
 

M = (N-1) + (T-K)
print(M)
for i in range(2, N+1):
    print(1, i)
count = 1
i,j = 2,3
while count <= T-K:
    print(i, j)
    if j < N:
        j += 1
    else:
        i += 1
        j = i + 1
    count +=1

Solution (Python):
from itertools import combinations
N, K = map(int, input().split())

if K > ((N - 1) * (N - 2)) // 2:
    print(-1)
    exit()

print(N - 1 + ((N - 1) * (N - 2)) // 2 - K)
for i in range(2, N + 1):
    print(1, i)

t=list(combinations([j+2 for j in range(N-1)],2))
for i in range(((N - 1) * (N - 2)) // 2 - K):
    print(*t[i])

Solution (Python):
import sys
a, b = [int(i) for i in input().split()]

if b > (a-1)*(a-2)//2:
    print(-1)
    sys.exit()

c = []
cur = 1
while cur < a:
    for i in range(1, a + 1):
        if cur < i:
            c.append([cur, i])
    cur += 1

for _ in range(b):
    c.pop()

print(len(c))

for i in c:
    print(int(i[0]), int(i[1]))

Solution (Python):
import sys
from collections import deque  
from collections import defaultdict  
from heapq import heapify, heappush, heappop, heappushpop  
from bisect import bisect_left, bisect_right  



s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N, K = i2nn()
    

    
    
    
    
    
    
    
    

    
    
    
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    NM = (N-1) * (N-2) / 2
    if K > NM:
        print(-1)
        return
    
    UV = [(1, i) for i in range(2, N+1)]
    for i in range(2, N):
        if K >= NM:
            break
        for j in range(i+1, N+1):
            if K >= NM:
                break
            UV.append((i, j))
            K += 1

    M = len(UV)
    print(M)
    for u, v in UV:
        print(u, v)

main()

Solution (Python):
n,k=map(int,input().split())
d=[]
t=(n-1)*(n-2)//2
if t<k:
    print(-1)
    exit()
for i in range(2,n+1):
    d.append((1,i))
t-=k
for i in range(2,n):
    if t==0: break
    for j in range(i+1,n+1):
        d.append((i,j))
        t-=1
        if t==0: break
print(len(d))
for i in d:
    print(i[0],i[1])

Solution (Python):
from itertools import islice


N, K = map(int, input().split())


S = (N - 1) * (N - 2) // 2
D = S - K

ans = (
    -1 if K > S else
    '{}\n{}'.format(
        (N - 1) + D,
        '\n'.join(
            '{} {}'.format(u, v)
            for u, v in [
                (1, i)
                for i in range(2, N + 1)
            ] + [
                (u, v)
                for u, v in islice(
                    (
                        (x, y)
                        for x in range(2, N + 1)
                        for y in range(x + 1, N + 1)
                    ),
                    D
                )
            ]
        )
    )
)


print(ans)

Solution (Python):
import itertools

N,K = map(int,input().split())

x = (N-1)*(N-2)//2

if x < K:
    print(-1)
    exit()
    
edge = [(1,n) for n in range(2,N+1)]
edge += list(itertools.combinations(range(2,N+1), 2))[:x-K]


print(len(edge))
for x,y in edge:
    print(x,y)

Solution (Python):


N, K = map(int, input().split())

lower = 0
upper = (N-1) * (N-2) / 2

ans = []

if lower <= K  and K <= upper:

    for i in range(1, N):
        for j in range(i+1, N+1):
            ans.append((i, j))

    k = 0
    while k < K:
        del ans[-1]
        k += 1

    print(len(ans))

    for ptn in ans:
        print('{} {}'.format(ptn[0], ptn[1]))


    
    
    

    
    
    
    

    
    

else:
    print('-1')

Solution (Python):
N, K = map(int, input().split())

lower = 0
upper = ((N-1) * (N-2)) // 2

ans = []

if lower <= K  and K <= upper:

    for i in range(N-1):
        ans.append((i+1, N))

    add = upper - K

    remains = []
    for i in range(N-1):
        for j in range(i):
            remains.append((i+1, j+1))
    
    for i in range(add):
        ans.append(remains[i])

    print(len(ans))
    for a in ans:
        print('{} {}'.format(a[0], a[1]))

else:
    print('-1')

Solution (Python):
def main():
    N, K = map(int, input().split())
    M = (N - 1) * (N - 2) // 2
    if K > M:
        print(-1)
        return
    ans = []
    for i in range(N - 1):
        ans.append((i + 1, N))
    E = []
    for i in range(N - 1):
        for j in range(i):
            E.append((i + 1, j + 1))
    for i in range(M - K):
        ans.append(E[i])
    print(len(ans))
    for i, j in ans:
        print(i, j)


main()

Solution (Python):
[N ,K] = list(map(int,input().split()))
max = int((N-1)*(N-2)/2)
if max<K:
    print(-1)
    exit()

tree = []
for i in range(2, N+1):
    tree.append((1,i))
t = 3
a=0
for i in range(0, max-K):
    if i+t-a>N:
        a += i-a
        t+=1 
    tree.append((i+2-a,i+t-a))

print(len(tree))
for i in tree:
    print(str(i[0]) + " " +str(i[1]))

Solution (Python):
def main():
	N, K = map(int, input().split())
	total = ((N-1)*(N-2)//2)
	if K > total:
		print(-1)
		return
	dif = total - K
	M = N-1+dif
	print(M)
	for i in range(N-1):
		print(" ".join([str(i+1), str(N)]))

	cur = 1
	add = 0
	for _ in range(dif):
		add += 1
		pair = cur+add
		if pair > N-1:
			cur += 1
			add = 1
			pair = cur+1
		print(" ".join([str(cur), str(pair)]))


if __name__ == '__main__':
	main()

Solution (Python):
import math

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


N, K = map(int, input().split())
if N > 2:
    x = combinations_count(N-1, 2)
else:
    x = 0
if x < K:
    print('-1')
    quit()

h = []
for i in range(2,N+1):
    h.append(('1', str(i)))

hen = x-K
x = 2
y = 3
while hen > 0:
    h.append((x,y))
    y += 1
    if y > N:
        x += 1
        y = x+1
    hen -= 1

print(len(h))
for x,y in h:
    print(x,y)

Solution (Python):

N, K = map(int, input().split())


if K > (N-1)*(N-2)/2:
    print(-1)
else:
    M = int((N-1)*(N-2)/2) - K

    print(M + N - 1)
    for i in range(2, N+1):
        print(1, i)
    for i in range(2, N+1):
        for j in range(i+1, N+1):
            if M > 0:
                M -= 1
                print(i, j)
            else:
                break
        if M == 0:
            break

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) 

def LI(): return list(map(int, sys.stdin.readline().split()))

N,K = LI()

if K > (N-1)*(N-2)//2 :
    print("-1")
    exit()
nodes = []
for i in range(1,N):
    nodes.append([1,i+1])
add = (N-1)*(N-2) // 2 - K
for i in range(N-2):
    for j in range(i,N-2):
        add -= 1
        if add < 0:
            break
        nodes.append([i+2,j+3])
    else:
        continue
    break

print(len(nodes))
for node in nodes:
    x,y = node
    print(x,y)

Solution (Python):
N, K = map(int, input().split())

upper = ((N-1)*(N-2))//2
if (upper < K):
    print('-1')
    exit()

ans = []

for i in range(N-1):
    ans.append((i+1,N))
add = upper - K
count = 0
for i in range(N-1):
    if count < add:
        for j in range(i):
            if count < add:
                ans.append((i+1, j+1))
                count += 1
            else:
                break
    else:
        break

print(len(ans))
for edge in ans:
    print("{} {}".format(edge[0], edge[1]))

Solution (Python):
N,K = map(int, input().split())

Kmax = (N-2)*(N-1)//2
if K > Kmax:
    print(-1)
    exit()

M = N-1+Kmax-K
print(M)

for i in range(2,N+1):
    print(1,i)

t = Kmax-K
s = 2
while t>0:
    if t > N-s:
        for i in range(N-s):
            print(s,s+1+i)
        t -= N-s
        s += 1
    else:
        for i in range(t):
            print(s,s+1+i)
        exit()

Solution (Python):
n, k = map(int, input().split())
m = (n - 1) * (n - 2) // 2
if k > m:
    print(-1)
else:
    print(n - 1 + m - k)
    for i in range(2, n + 1):
        print(1, i)
    t = 0
    for i in range(2, n + 1):
        for j in range(i + 1, n + 1):
            if t >= m - k:
                break
            print(i, j)
            t += 1

Solution (Python):
N,K=map(int,input().split())
MAX=(N-1)*(N-2)//2
if K>MAX:
    print(-1)
else:
    print(N-1+MAX-K)
    for i in range(N-1):
        print(1,2+i)
    from itertools import combinations
    n=MAX
    for a,b in combinations(range(2,N+1),2):
        if n==K:
            break
        print(a,b)
        n-=1

Solution (Python):
import itertools
import os
import sys

from scipy.misc import comb

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

N, K = list(map(int, sys.stdin.readline().split()))

max_k = comb(N - 1, 2, exact=True)
if K > max_k:
    print(-1)
    exit()
graph = []
for i in range(2, N + 1):
    graph.append((1, i))
count = max_k
for v in itertools.combinations(range(2, N + 1), r=2):
    if count <= K:
        break
    graph.append(v)
    count -= 1
print(len(graph))
for v, u in graph:
    print(v, u)

Solution (Python):
N, K = map(int, input().split())


def main():
    if K > (N - 2)*(N - 1) / 2:
        return 0
    
    
    G = [(0, i) for i in range(1, N)]
    

    
    num = (N - 2)*(N - 1) / 2 - K
    u = 1
    v = 2    
    while num >= 1:
        if v == N:
            u += 1
            v = u + 1
        G.append((u, v))

        v += 1
        num -= 1
    
    return G

if main():
    G = main()
    print(len(G))
    for i in range(len(G)):
        u, v = G[i]
        print("{} {}".format(u+1, v+1))

else:
    print("-1")

Solution (Python):
n,k=map(int,input().split())
if k>(n-1)*(n-2)//2:
    print(-1)
else:
    m=(n-1)+(n-1)*(n-2)//2-k
    print(m)
    for i in range(n-1):
        print(i+1,n)
    count=(n-1)*(n-2)//2-k
    import sys
    for i in range(1,n):
        for j in range(i+1,n):
            if count==0:
                sys.exit()
            print(i,j)
            count=count-1

Solution (Python):
n,k = map(int,input().split())

if k > (n-1)*(n-2)/2:
    print(-1)
else:
    l = [[1,_] for _ in range(2,n+1)]
    next = [2,3]
    d = int((n-1)*(n-2)/2) - k
    for i in range(d):
        l.append(next[:])
        if next[1] != n:
            next[1] += 1
        else:
            next[0] += 1
            next[1] = next[0] + 1
    print(len(l))
    for ll in l:
        print(ll[0],ll[1])

Solution (Python):



import itertools
N, K = map(int, input().split())
MAX = (N-1)*(N-2)//2
if K > MAX:
  print(-1)
  exit()


edge = [(1, n) for n in range(2, N+1)]
edge += list(itertools.combinations(range(2, N+1),2))[:MAX-K]

print(len(edge))
for x, y in edge:
  print(x,y)

Solution (Python):
from itertools import combinations
N,K=map(int,input().split())
L=(N-1)*(N-2)//2
if K>L:print(-1);exit()
M=N-1+L-K
print(M)
for e in list(combinations(range(1,N+1),2))[:M]:print(*e)

Solution (Python):
N, K = map(int, input().split())










mx = (N-1)*(N-2)/2
if mx < K:
    print("-1")
    exit()

ans = list()


for i in range(N-1):
    ans.append((i+1, N))


add = int(mx - K)
edge = list()
for i in range(N-1):
    for j in range(i):
        edge.append((j+1, i+1))


for i in range(add):
    ans.append(edge[i])

print(len(ans))
for a in ans:
    print("{} {}".format(a[0], a[1]))

Solution (Python):
from itertools import combinations

N,K = map(int, input().split())

MAX_K = (N-1)*(N-2)//2
if K > MAX_K:
    print(-1)
    exit()

M = N-1 + (MAX_K - K)
print(M)
edges = list(combinations(range(1, N+1), 2))[:M]

for e in edges:
    print(*e)

Solution (Python):
n,k = map(int,input().split())
m = (n-1)*(n-2)//2
if( m<k ):
    print(-1)
    exit(0)

ans = []
buf = []
sub = m-k
for i in range(2,n+1):
    ans.append((1,i))

for i in range(3,n+1):
    for j in range(2,i):
        buf.append((j,i))

ans.extend(buf[:sub])

print(len(ans))
for x,y in ans:
    print('{0} {1}'.format(x,y))