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())

M=(N-1)*(N-2)//2
if K<=M:
    
    print(N-1+M-K)
    for i in range(1,N):
        print(i,N)
    i,j=1,2
    for _ in range(M-K):
        print(i,j)
        j+=1
        if j==N:
            i+=1
            j=i+1
else:
    
    print(-1)

Solution (Python):
"""
まず、最短距離が2であるような点の組み合わせの数が最も多くなるように木を作る。
そのあと、最短距離が2であるような点の組み合わせの数がKになるまで、辺を増やしていく。
"""

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

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

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

cnt = (N-1)*(N-2)//2
for i in range(1,N):
    for j in range(i+1,N):
        if cnt == K:
            break
        ans.append((str(i),str(j)))
        cnt -= 1
    else:
        continue
    break
print(len(ans))
for a in ans:
    print(" ".join(a))

Solution (Python):


import sys 
from itertools import combinations
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, K = map(int,readline().split())
if K > (N-1)*(N-2)//2:
    print(-1)
else:
    
    x = (N-1)*(N-2)//2
    rest = x-K
    ans = [(x,N) for x in range(1,N)] + list(combinations(range(1,N),2))
    ans = ans[:N-1+rest]
    print(len(ans))
    for x,y in ans:
        print(x,y)

Solution (Python):


import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, K = map(int,readline().split())
if K > (N-1)*(N-2)//2:
    print(-1)
else:
    
    ans = []
    for i in range(1,N):
        ans.append((i,N))
    M = N*(N-1)//2 - K
    cnt = N-1
    flag = False
    for i in range(1,N):
        for j in range(i+1,N):
            if cnt == M:
                flag = True
                break
            ans.append((i,j))
            cnt += 1
        if flag:
            break
    print(M)
    for x,y in ans:
        print(x,y)

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

def main(n,k):
    f = [1]
    for i in range(n):
        f.append(f[i]*(i+1))
    maxpair = f[n-1]//(f[2]*f[n-3])
    if k>maxpair:
        print(-1)
    else:
        pair = maxpair
        ans = []
        for i in range(n-1):
            ans.append([1,i+2])
        for i in range(2,n+1):
            for j in range(i,n+1):
                if pair==k:
                    break
                if i==j:
                    continue
                ans.append([i,j])
                pair -= 1
        print(len(ans))
        for i in ans:
            print(str(i[0]) + " " + str(i[1]))

if __name__=="__main__":
    n,k = getval()
    main(n,k)

Solution (Python):
import itertools

N, K = map(int, input().split())
ans = []
for i in range(2, N + 1):
    ans.append([1, i])

"""
全ての組み合わせは N_C_2
1を中心にスターグラフを作る
2,3,...,Nの中での組み合わせは N-1_C_2、これらの距離は2
2,3,...,Nの中で、(N-1_C_2 - K) 個の辺を張れば、
距離2の組み合わせがK個残る

N-1_C_2 - K < 0 となる時、構築不可
"""

if (N - 1) * (N - 2) // 2 - K < 0:
    print(-1)
    exit()
else:
    comb = list(itertools.combinations(range(2, N + 1), 2))
    for i in range((N - 1) * (N - 2) // 2 - K):
        u = comb[i][0]
        v = comb[i][1]
        ans.append([u, v])
    print(len(ans))
    for i in ans:
        print(*i)

Solution (Python):
import itertools
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
ans = []
for i in range(2, N + 1):
    ans.append([1, i])

"""
全ての組み合わせは N_C_2
1を中心にスターグラフを作る
2,3,...,Nの中での組み合わせは N-1_C_2、これらの距離は2
2,3,...,Nの中で、(N-1_C_2 - K) 個の辺を張れば、
距離2の組み合わせがK個残る

N-1_C_2 - K < 0 となる時、構築不可
"""

if (N - 1) * (N - 2) // 2 - K < 0:
    print(-1)
    exit()
else:
    comb = list(itertools.combinations(range(2, N + 1), 2))
    for i in range((N - 1) * (N - 2) // 2 - K):
        u = comb[i][0]
        v = comb[i][1]
        ans.append([u, v])
    print(len(ans))
    for i in ans:
        print(*i)

Solution (Python):
import itertools

N, K = map(int, input().split())
ans = []
for i in range(2, N + 1):
    ans.append([1, i])

"""
全ての組み合わせは N_C_2
1を中心にスターグラフを作る
2,3,...,Nの中での組み合わせは N-1_C_2、これらの距離は2
2,3,...,Nの中で、(N-1_C_2 - K) 個の辺を張れば、
距離2の組み合わせがK個残る

N-1_C_2 - K < 0 となる時、構築不可
"""

if (N - 1) * (N - 2) // 2 - K < 0:
    print(-1)
    exit()
else:
    comb = list(itertools.combinations(range(2, N + 1), 2))
    for i in range((N - 1) * (N - 2) // 2 - K):
        u = comb[i][0]
        v = comb[i][1]
        ans.append([u, v])
    print(len(ans))
    for i in ans:
        print(*i)

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N, K = LI()

    
    
    
    max_K = N * (N - 1) // 2 - (N - 1)
    if K <= max_K:
        
        
        print((N - 1) + max_K - K)
        for i in range(1, N):
            print(i, N)
        cnt = max_K
        for i, j in itertools.combinations(range(1, N), 2):
            if cnt == K:
                break
            print(i, j)
            cnt -= 1
    else:
        print(-1)

if __name__ == '__main__':
    resolve()

Solution (Python):
N,K=[int(x) for x in input().split()]
if K>(N-2)*(1+N-2)//2:
	print(-1)
else:
	print(((N-2)*(1+N-2)//2)+((N-1)-K))
	counter=0
	for i in range(2,N+1):
		print(1,i)
		counter+=1
	for i in range(2,N+1):
		for j in range(i+1,N+1):
			if counter==((N-2)*(1+N-2)//2)+((N-1)-K):
				exit()
			else:
				print(i,j)
				counter+=1

Solution (Python):
def connect(n,k,ans,edge_list):
	for u in range(1,n):
		for v in range(u+1,n):
			if k==ans:
				return
			else:
				edge_list.append((u,v))
				ans -= 1

def solve(n,k):
	edge_list = []
	ans = 0

	for u in range(1,n):
		ans += len(edge_list)
		edge_list.append((u,n))

	if k > ans:
		print(-1)
		return

	connect(n,k,ans,edge_list)
	print(len(edge_list))
	for edge in edge_list:
		print(*edge)

solve(*map(int,input().split()))

Solution (Python):
import sys
import itertools

import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import permutations
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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

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

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

for i in range(2, N + 1):
    if now == K:
        break
    for j in range(i + 1, N + 1):
        if now == K:
            break
        es.append((i, j))
        now -= 1

print(len(es))
for a, b in es:
    print(a, b)

Solution (Python):
N, K = map(int,input().split())
if K > (N-1)*(N-2)//2:
    print(-1)
    exit()
M = N-1
cnt = (N-1)*(N-2)//2
edges = []
for i in range(2, N+1):
    edges.append((1, i))
for i in range(2, N+1):
    for j in range(i+1, N+1):
        if cnt == K:
            break
        edges.append((i, j))
        cnt -= 1
        M += 1
    if cnt == K:
        break
print(M)
for u, v in edges:
    print(u, v)

Solution (Python):
n, k = map(int, input().split())
m = 0
e = []
for i in range(n-1):
    e.append((1, i+2))
    m += 1

rep = (n-1)*(n-2)//2 - k
if rep < 0:
    print(-1)
    exit()
e_list = []
for i in range(2, n+1):
    for j in range(i+1, n+1):
        if rep == 0:
            print(m)
            for edge in e:
                print(*edge)
            exit()
        e.append((i, j))
        m += 1
        rep -= 1

if rep == 0:
    print(m)
    for edge in e:
        print(*edge)

Solution (Python):
"""
最短距離が2であるようなノードの組み合わせは、最高で(N-1)C2個存在する。
そこで、この(N-1)C2個の組み合わせがある状態をデフォルトとして、K個になるまで辺を増やしていく。
"""

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

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

rest = (N-1)*(N-2)//2 - K
for i in range(2,N+1):
    for j in range(i+1,N+1):
        if rest == 0:
            break
        else:
            edges.append((str(i),str(j)))
            rest -= 1
    else:
        continue
    break
print(len(edges))
for edge in edges:
    print(" ".join(edge))

Solution (Python):
n,k = map(int,input().split())
val = (n-1)*(n-2)//2
if k > val:print(-1);exit()


print(n-1+val-k)

for i in range(2,n+1):
    print('1' + ' ' + str(i))

v = 2
w = 3
while k < val:
    val -= 1
    print(v, w)
    w += 1
    if w == n + 1:
        v += 1
        w = v + 1

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = map(int, readline().split())

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

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

    cnt = 0
    for i in range(1, N):
        done = False
        for j in range(i + 1, N):
            if cnt == M - K:
                done = True
                break
            ans.append((i, j))
            cnt += 1
        if done:
            break

    print(len(ans))
    for i, j in ans:
        print(i, j)

    return


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import combinations
n, k = map(int, input().split())
if k > ((n-1)*(n-2))//2:
    print(-1)
    exit()

edges = []
for i in range(2, n+1):  
    edges.append((1, i))
dist_2 = ((n-1)*(n-2))//2
list_2 = list(combinations(range(2, n+1), 2))

for i in range(dist_2-k):
    edges.append(list_2[i])


def ans(lis):
    print(len(lis))
    for x, y in lis:
        print(x, y)
ans(edges)

Solution (Python):
N, K = [int(x) for x in input().split()]

ok = False
if K <= (N - 1)*(N - 2)//2:
    ok = True
    e = N*(N - 1)//2 - K
    edge = []
    cnt = 0
    done = False
    for i in range(1, N):
        for j in range(i + 1, N + 1):
            edge.append([i, j])
            cnt += 1
            if cnt == e:
                done = True
                break
        if done == True:
            break

if ok == True:
    print(e)
    for x in edge:
        print(x[0], end=' ')
        print(x[1])
else:
    print(-1)

Solution (Python):
from itertools import combinations

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

if K > (N-1)*(N-2)//2:
  print(-1)
else:
  M = N*(N-1)//2 - K
  print(M)
  for i in range(2, N+1):
    print(1, i)
  M -= N-1
  for ab in combinations(range(2, N+1), 2):
    if M == 0:
      break
    else:
      a, b = ab
      print(a, b)
      M -= 1

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

print(n - 1 + now - k)
for i in range(2, n + 1):
    print(1, i)
v = 2
w = 3
while k < now:
    now -= 1
    print(v, w)
    w += 1
    if w == n + 1:
        v += 1
        w = v + 1

Solution (Python):
"""
Nの制約数の少なさてきに、全探索するのがよさそう。
とはいえ、ありうるすべてのグラフパターンを全探索したら、2**(N^2)かかる

N本目以降は、辺の数を増やせば増やすほど、有効なペアの数が減っていく。
逆にいうと、ペアの数を減らしたい場合は、N本目以降を追加する
"""
N,K = map(int,input().split())
if K > N*(N-1)//2-(N-1):
    print(-1)
    exit()
edges = []
base = 1
for i in range(base+1,N+1):
    edges.append(" ".join([str(base),str(i)]))

diff = (N-1)*(N-2)//2 - K
base = 2
while diff:
    for i in range(base+1,N+1):
        if diff == 0:
            break
        else:
            edges.append(" ".join([str(base),str(i)]))
            diff -= 1
    base += 1
print(len(edges))
for ans in edges:
    print(ans)

Solution (Python):
from itertools import combinations
from collections import deque
n,k = map(int,input().split())
i_canerase = [n-2] * n
import sys
q = deque(combinations([i for i in range(n)],2))
use_edges = []
canmake = 0
while q:
    if canmake == k:
        use_edges += list(q)
        print(len(use_edges))
        for i in range(len(use_edges)):
            a,b = use_edges[i]
            print(a+1,b+1)
        sys.exit()

    a,b = q.popleft()
    if i_canerase[a]>0 and i_canerase[b]>0:
        i_canerase[a] -= 1
        i_canerase[b] -= 1
        canmake += 1
    else:
        use_edges.append([a,b])

print(-1)

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

if K > M:
    print(-1)
else:
    ans = N-1 + M - K
    print(ans)

    for i in range(1,N):
        print(i,N)
        ans -= 1

    for i in range(1,N):
        for j in range(i+1,N):
            if ans == 0:
                exit()
            else:
                print(i,j)
                ans -= 1

Solution (Python):
from itertools import combinations
n, k = map(int, input().split())
nc2 = (n - 1) * (n - 2) // 2
if nc2 < k:
  print('-1')
  exit()
print(n - 1 + (nc2 - k))  
for i in range(2, n+1):
  print(f'1 {i}')
for i in range(nc2 - k):
  combs = list(combinations(range(2, n+1), 2))
  print(f'{combs[i][0]} {combs[i][1]}')

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


import heapq
sys.setrecursionlimit(10**6)

INF = 10**18
import bisect
import statistics
mod = 10**9+7


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

if N < 0 or K > (N-1)*(N-2)//2:
    print(-1)
else:
    M = (N-1)*(N-2)//2 - K
    print(M+N-1)
    for i in range(1,N):
        print(i,N)
    count = 0
    for i in range(1,N):
        for j in range(i+1,N):
            if count == M:
                sys.exit()
            print(i,j)
            count += 1

Solution (Python):
n,k=map(int,input().split())
ret=[str(i)+' 1'for i in range(2,n+1)]

m=(-2+n)*(n-1)//2-k
if m<0:print(-1);exit()
now=2
j=3
print(len(ret)+m)
while m:
    ret.append(str(now)+' '+str(j))
    j+=1
    if j==n+1:
        j=now+2
        now+=1
    m-=1
print("\n".join(ret))

Solution (Python):
N, K = map(int, input().split())
abs_g = [(i, j) for i in range(1, N) for j in range(i + 1, N + 1)][::-1]

if len(abs_g) - (N - 1) < K:
    print(-1)
    exit()

print(len(abs_g) - K)
[print(i, j) for i, j in abs_g[K:]]

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

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



if k <= (n - 1) * (n - 2) // 2:
    
    hen = []
    for i in range(2, n + 1):
        hen.append((1, i))
    m = (n - 1) * (n - 2) // 2 - k
    while m:
        for i in range(2, n + 1):
            for j in range(i + 1, n + 1):
                hen.append((i, j))
                m -= 1
                if m == 0:
                    break
            if m == 0:
                break
    print(len(hen))
    for a, b in hen:
        print(a, b)

else:
    print(-1)

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








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

com = (n-1)*(n-2)//2
rem = com - k
if rem < 0:
    print(-1)
    exit()

cu = 2
cv = 3
for i in range(rem):
    if cv > n:
        cu += 1
        cv = cu+1
    ansl.append((cu,cv))
    cv += 1

print(len(ansl))
for u,v in ansl:
    print(u,v)

Solution (Python):
def main(N, K):
    e_max = (N - 1) * (N - 2) // 2
    if K > e_max:
        print(-1)
        return

    edge = []
    add = []
    for i in range(2, N + 1):
        edge.append((1, i))
    for i in range(2, N):
        for j in range(i + 1, N + 1):
            add.append((i, j))

    while e_max != K:
        edge.append(add.pop())
        K += 1

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


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

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()


def main():
    n, k = map(int, input().split())
    full = (n-1)*(n-2)//2
    if k > full:
        print(-1)
    else:
        num = full-k
        print(num+n-1)
        for i in range(2, n+1):
            print(1, i)
        for i in range(2, n):
            for j in range(i+1, n+1):
                if num == 0:
                    exit()
                print(i, j)
                num -= 1


if __name__ == '__main__':
    main()

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

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


uvs = [(i, N) for i in range(1, N)]

to_decrease = (N - 1) * (N - 2) // 2 - K
for i in range(1, N):
    for j in range(i+1, N):
        if to_decrease == 0:
            break
        uvs.append((i, j))
        to_decrease -= 1
    else:
        continue
    break

print(len(uvs))
for u, v in uvs:
    print(u, v)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
def main():
    n, k = list(map(int, input().split()))
    a = (n-1)*(n-2)//2
    if k > a:
        print('-1')

    else:
        print(n-1+a-k)
        
        for i in range(2, 1+n):
            print(1, i)

        cnt = 0
        for i in range(2, n):
            for j in range(i+1, n+1):
                if cnt >= a-k:
                    return
                print(i, j)
                cnt += 1

main()

Solution (Python):
n,k=map(int,input().split())
use=[(i,j) for i in range(2,n) for j in range(i+1,n+1)]
if k>(n-2)*(n-1)//2:
  print(-1)
  exit()
ans=[]
for i in range(2,n+1):
  ans+=[(1,i)]
for _ in range((n-2)*(n-1)//2-k):
  ans+=[use.pop()]
print(len(ans))
for i in ans:
  print(*i)

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n, k = LI()
if k > (n - 1) * (n - 2) // 2:
    print(-1)
else:
    print((n - 1) * (n - 2) // 2 - k  + (n - 1))
    edges = []
    for i in range(2, n+1):
        edges.append((1, i))
    cnt = 0
    for i in range(2, n+1):
        for j in range(i+1, n+1):
            if cnt == (n - 1) * (n - 2) // 2 - k:
                break
            edges.append((i, j))
            cnt += 1
        else:
            continue
        break
    for u, v in edges:
        print(u, v)

Solution (Python):
N, K = map(int, input().split())
if K> ((N-1)*(N-2))//2:
    print(-1)
else:
    p = []
    q = []
    for t in range(2, N):
        for u in  range(t+1, N+1):
            q.append([t, u])
    for i in range(2, N+1):
        p.append([1, i])
    for j in range(((N-1)*(N-2))//2 - K):
        p.append(q[j])
    print(len(p))
    for y in p:
        print(*y)

Solution (Python):
N,K = map(int,input().split())
if N==2:
    if K==0:
        print(1)
        print(1,2)
    elif K==1:
        print(-1)
elif N==3:
    if K==0:
        print(3)
        print(1,2)
        print(2,3)
        print(1,3)
    elif K==1:
        print(2)
        print(1,2)
        print(1,3)
    elif K>=2:
        print(-1)
elif N==4:
    if K==0:
        print(6)
        print(1,2)
        print(1,3)
        print(1,4)
        print(2,3)
        print(2,4)
        print(3,4)
    elif K==1:
        print(5)
        print(1,3)
        print(1,4)
        print(2,3)
        print(2,4)
        print(3,4)
    elif K==2:
        print(4)
        print(1,3)
        print(1,4)
        print(2,4)
        print(3,4)
    elif K>=3:
        print(-1)
else:
    if K>((N-1)*(N-2))//2:
        print(-1)
    else:
        A = [(i,j) for i in range(1,N) for j in range(i+1,N+1)]
        i = 2
        j = 3
        cnt = 0
        while cnt<K:
            if (i,j) in A:
                A.remove((i,j))
            j += 1
            if j>N:
                i += 1
                j = i+1
            cnt += 1
        print((N*(N-1))//2-K)
        for a in A:
            print(a[0],a[1])

Solution (Python):
def output(X):
  for x in X:
    print(*x)

N,K = map(int,input().split())
ALL = N*(N-1)//2
MAX = ALL - (N-1)
if K > MAX:
  print(-1)
  exit()
G = []
req = ALL-K
cnt = 0
for i in range(1,N+1): 
  for j in range(i+1,N+1): 
    if i == 1:
      G.append([i,j])
      cnt += 1
    else:
      if cnt >= req:
        print(req)
        output(G)
        exit()
      else:
        G.append([i,j])
        cnt += 1
print(req)
output(G)

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

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

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

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

for i in range(2, N):
    for j in range(i + 1, N + 1):
        if add:
            print(i, j)
            add -= 1

Solution (Python):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,K=MI()
    
    
    N2=((N-1)*(N-2))//2
    
    if K>N2:
        print(-1)
        exit()
    
    E=[]
    for i in range(1,N):
        E.append([0,i])
        
    rem=N2-K
    
    for i in range(1,N):
        for j in range(i+1,N):
            if rem==0:
                break
            E.append([i,j])
            rem-=1
            
    M=len(E)
    print(M)
    
    for u,v in E:
        print(u+1,v+1)
            
    
    
            
    

main()

Solution (Python):
'''
自宅用PCでの解答
'''
import math

import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    n,k = map(int,ipt().split())
    if k > (n-1)*(n-2)//2:
        print(-1)
    else:
        num = (n-1)*(n-2)//2-k
        print(num+(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 num == 0:
                    break
                print(i,j)
                num -= 1
    


    return None

if __name__ == '__main__':
    main()

Solution (Python):
N,K = map(int,input().split())
if K > (N-1)*(N-2)//2:
    print(-1)
else:
    edges = []
    for i in range(2,N+1):
        edges.append((1,i))
    cur = (N-1)*(N-2)//2
    if cur > K:
        for i in range(2,N+1):
            for j in range(2,N+1):
                if i > j:
                    edges.append((i,j))
                    cur -= 1
                    if cur == K:
                        break
            else:
                continue
            break
    print(len(edges))
    for i,j in edges:
        print(i,j)

Solution (Python):

from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools, bisect
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [SL() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]

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

alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

sys.setrecursionlimit(1000000)
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007
INF = float("inf")

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

    if (N == 2 and K == 0):
        print(1)
        print(1, 2)
        return

    if ((N-1 < 2) or comb(N-1, 2) < K):
        print(-1)
        return
    maxpairs = comb(N-1, 2)

    edges = [[] for _ in range(N)]
    edges[0] = list(range(1, N))

    pairs = list(itertools.combinations(range(1, N), 2))

    for _ in range(maxpairs - K):
        a, b = pairs.pop()
        edges[a].append(b)

    ans = []
    for a in range(N):
        for b in edges[a]:
            ans.append((a+1, b+1))

    print(len(ans))
    for a, b in ans:
        print(a, b)

if __name__ == '__main__':
    main()

Solution (Python):
from itertools import combinations

n, k = map(int, input().split())

border = (n - 1) * (n - 2) // 2

if k > border:
  print(-1)
else:
  print(n - 1 + border - k)
  comb = list(combinations([i for i in range(1, n + 1)], 2))
  for i, j in comb[:n - 1 + border - k]:
    print(i, j)

Solution (Python):
n,k = map(int,input().split())
res_graph = [[1,i] for i in range(2,n+1)]
res = (n-1)*(n-2)//2

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

tmp_edge = 2
while res!=k:
    tmp = 1
    for i in range(n-tmp_edge):
        res_graph.append([tmp_edge,tmp+tmp_edge])
        res-=1
        tmp+=1
        if res==k:
            break
    tmp_edge+=1

print(len(res_graph))
for i,j in res_graph:
    print(i,j)

Solution (Python):
from itertools import combinations

n, k = map(int, input().split())

border = (n - 1) * (n - 2) // 2

if k > border:
  print(-1)
else:
  print(n - 1 + border - k)
  for i in range(2, n + 1):
    print(1, i)
  comb = list(combinations([i for i in range(2, n + 1)], 2))
  for i, j in comb[:border - k]:
    print(i, j)

Solution (Python):
from itertools import combinations

n, k = map(int, input().split())

if k > (n - 1) * (n - 2) // 2:
  print(-1)
else:
  print(n - 1 + (n - 1) * (n - 2) // 2 - k)
  for i in range(2, n + 1):
    print(1, i)
  comb = list(combinations([i for i in range(2, n + 1)], 2))
  for i, j in comb[:(n - 1) * (n - 2) // 2 - k]:
    print(i, j)

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

a=(n-2)*(n-1)//2
print(n-1-k+a)
for i in range(1,n):
    print(1,i+1)

A=[]
for i in range(1,n):
    for j in range(1,i):
        A.append([i+1,j+1])
        
for i in range(a-k):
    print(*A[i])

Solution (Python):
N,K=map(int,input().split())
fil=int((N-1)*(N-2)/2)
if K>fil:
  print(-1)
else:
  M=N-1+fil-K
  print(M)
  for i in range(N-1):
    print(1,i+2)
    count=0
  list=[]

  for j in range(2,N):
    for k in range(j+1,N+1):
      list.append((j,k))
  for i in range(fil-K):
    print(list[i][0],list[i][1])

Solution (Python):
import sys
from io import StringIO
import unittest
import os
import itertools


sys.setrecursionlimit(999999999)



def resolve(test_def_name=""):
    n, k = map(int, input().split())

    if (n * (n - 1)) // 2 - (n - 1) < k:
        print(-1)
        return

    
    points = [i for i in range(1, n + 1)]

    
    e = [(1, point) for point in points if not point == 1]
    
    cnt = len(e)

    
    addables = itertools.combinations(points[1:], 2)

    
    
    
    
    adds = list(addables)[0:(n * (n - 1)) // 2 - (n - 1) - k]
    e += adds
    cnt += len(adds)

    print(cnt)

    for aa in e:
        print(str(aa[0]) + " " + str(aa[1]))





class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """5 3"""
        output = """5
4 3
1 2
3 1
4 5
2 3"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """5 8"""
        output = """-1"""
        self.assertIO(test_input, output)

    
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)



if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        
        resolve()

    else:
        
        unittest.main()

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

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

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

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

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

print(len(ans))
for u,v in ans:
  print(u,v)

Solution (Python):
N, K = map(int, input().split())
P = (N-1)*(N-2)//2
if K > P:
    print(-1)
    exit()
M = (N-1)+ (P-K)
print(M)
for i in range(2,N+1):
    print(1, i)
cnt = 1
i = 2
j = i+1
while cnt <= P-K:
    if j > N:
        i+= 1
        j = i+1
    print(i, j)
    j += 1
    cnt += 1

Solution (Python):
import sys
input = sys.stdin.buffer.readline




def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))


def main():
	n,k=MI()

	kk=(n-1)*(n-2)//2-k

	if kk<0:
		print(-1)
		exit()

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

	for i in range(1,n):
		for j in range(i+1,n):
			if kk>0:
				kk-=1
				ans.append([i,j])

	print(len(ans))
	for i,j in ans:
		print(i,j)













if __name__ == "__main__":
	main()