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):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
sys.setrecursionlimit(10 ** 9)



INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n, k = LI()
if k > (n - 1) * (n - 2) // 2:
    print(-1)
    exit(0)


ans = []
for i in range(2, n + 1):
    ans += [(1, i)]
ans += list(combinations(range(2, n + 1), 2))[:(n - 1) * (n - 2) // 2 - k]
print(len(ans))
for j in ans:
    print(*j)

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

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


if M < N-1:
    print(-1)
    exit()

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

Solution (Python):

import sys




import itertools





def main():
    n, k = [int(x) for x in sys.stdin.readline().split()]
    
    
    
    if k > (n - 1) * (n - 2) // 2:
        print(-1)
        sys.exit()

    all_uv = list(itertools.combinations(range(1, n+1), 2))

    m = n * (n - 1) // 2 - k
    print(m)
    
    for u, v in all_uv[:m]: 
        print(u, v)


if __name__ == "__main__":
    main()

Solution (Python):

import sys




import itertools





def main():
    n, k = [int(x) for x in sys.stdin.readline().split()]
    
    
    
    if k > (n - 1) * (n - 2) // 2:
        print(-1)
        sys.exit()
        
    all_uv = list(itertools.combinations(range(1, n+1), 2))

    m = n * (n - 1) // 2 - k
    print(m)
    
    for u, v in all_uv[:n*(n-1)//2-k]: 
        print(u, v)


if __name__ == "__main__":
    main()

Solution (Python):

import sys




import itertools





def main():
    n, k = [int(x) for x in sys.stdin.readline().split()]
    
    
    
    if k > (n - 1) * (n - 2) // 2:
        print(-1)
        sys.exit()
    all_combs = list(itertools.combinations(range(1, n+1), 2))
    
    m = n * (n - 1) // 2 - k
    print(m)
    for i in range(n*(n-1)//2-k): 
        print(all_combs[i][0], all_combs[i][1])


if __name__ == "__main__":
    main()

Solution (Python):
n,k=map(int,input().split())
m=(n-1)*(n-2)//2
if k>m:
    print(-1)
else:
    l=m-k
    lst=[]
    for i in range(1,n):
        lst.append([i,n])
    for i in range(1,n):
        for j in range(i+1,n):
            if l==0:
                break
            lst.append([i, j])
            l-=1
        else:
            continue
        break
    print(len(lst))
    for a,b in lst:
        print(a,b)

Solution (Python):
n,k=map(int,input().split())
if k > (n-1)*(n-2)//2 :
    print(-1)
else:
    print((n-1)*(n-2)//2 - k+n-1)
    for i in range(2,n+1):
        print("1 "+str(i))
    res = []
    for i in range(2,n):
        for j in range(i+1,n+1):
            res.append(str(i)+" "+str(j))
    print("\n".join(res[:((n-1)*(n-2)//2) - k]))

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

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

print(n-1+m)
for i in range(n-1):
  print(1,i+2)

if m>0:
  for i in range(2,n):
    for j in range(i+1,n+1):
      print(i, j)
      m -= 1
      if m==0:
         exit()

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

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

E = []
for i in range(2, n+1):
    E.append((1, i))
E_add = list(combinations(range(2, n+1), 2))
for i in range(m-k):
    E.append(E_add[i])

print(len(E))
for e in E:
    print(*e)

Solution (Python):
n,k=map(int,input().split())
m=(n-1)*(n-2)/2
ans=[]
if m<k:
  print(-1)
  exit()
else:
  for i in range(2,n+1):
    ans.append([1,i])
  a=m-k
  for i in range(2,n+1):
    for j in range(i+1,n+1):
      if a>0:
        a-=1
        ans.append([i,j])
      else:
        continue

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

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


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

num = (n-1)*(n-2)/2 - k
print(int(n-1+num))
ansl = []
for i in range(1,n):
    print(str(1)+" "+str(i+1))

for i in range(1,n):
    for j in range(i+1, n):
        if num > 0:
            print(str(i+1)+" "+str(j+1))
            num -= 1

Solution (Python):



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

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

if cnt<0:
    print(-1)
    sys.exit()
edg_ad = []
for v in range(2,n+1):
    if cnt == 0:
        break
    for u in range(2,v):
        if cnt == 0:
            break
        edg_ad.append((u,v))
        cnt -= 1
ans = []
for v in range(2,n+1):
    ans.append((1,v))
ans2 = set(ans)|set(edg_ad)
ans2 = list(ans2)
ans2.sort()
print(len(ans2))
for e in ans2:
    print(*e)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,k=map(int,input().split())
    s=(n-1)*(n-2)//2-k
    if(s<0):
        print(-1)
        return
    print(s+n-1)
    from itertools import product
    E=[(u,v) for u,v in product(range(2,n+1),repeat=2) if u<v]
    for i in range(n-1):
        print(1,i+2) 
    for i in range(s):
        print(*E[i])
resolve()

Solution (Python):

from itertools import combinations


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



max_pairs = int((N - 1) * (N - 2) / 2)
if K > max_pairs:
    print(-1)
    exit(0)



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


for i in range(1, N):
    print(i, N)
if K == max_pairs:
    exit(0)


pairs = max_pairs
for a, b in combinations(range(1, N), 2):
    print(a, b)
    pairs -= 1
    if pairs == K:
        exit(0)

Solution (Python):
import sys,os,math
from collections import Counter, defaultdict, deque
import bisect
from sys import stdin, stdout
from itertools import repeat
import Queue








def main():
    n, k = map(int, raw_input().split())
    ans = []
    if k > (n-1)*(n-2)/2:
        print -1
        return
    for i in range(1, n):
        ans.append((n, i))
    k -= (n-1)*(n-2)/2
    for i in range(1, n):
        for j in range(i+1, n):
            if k>=0:
                continue
            k += 1
            ans.append((i, j))
    print len(ans)
    print '\n'.join(map(lambda a:"%s %s"% (a[0],a[-1]), ans))
        


main()

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

if K > (N-1)*(N-2)//2:
    print(-1)
else:
    L = []
    for i in range(N-1):
        L.append(["1", str(i+2)])
    k = (N-1)*(N-2)//2 - K
    i = 2
    j = 3
    while k > 0:
        L.append([str(i), str(j)])
        j += 1
        if j == N+1:
            i += 1
            j = i+1
        k -= 1
        
    print(len(L))
    for i in range(len(L)):
        print(" ".join(L[i]))

Solution (Python):
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(1,N):
    print(i,N)


cnt = 0
for i in range(1,N):
    
    for j in range(i+1,N):
        if (N-1)*(N-2)//2 - cnt == K: exit()
        print(i,j)
        cnt += 1

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

if K <= M:
    edges = []
    for i in range(2, N + 1):
        edges.append((1, i))
    dis = M - K
    for i in range(2, N + 1):
        for j in range(i + 1, N + 1):
            if dis > 0:
                edges.append((i, j))
                dis -= 1
            else:
                break
        else:
            continue
        break
    print(len(edges))
    for fr, to in edges:
        print(fr, to)
else:
    print(-1)

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

import itertools

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

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

rest = x-K
UV = [(x,N) for x in range(1,N)] + list(itertools.combinations(range(1,N),2))
UV = UV[:N-1+rest]

print(len(UV))
print('\n'.join('{} {}'.format(x,y) for x,y in UV))

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

if K > (N-1)*(N-2)//2:
    print(-1)
else:
    ans = []
    for i in range(N - 1):
        ans.append([i+1, N])
    add = (N-1)*(N-2)//2 - 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 i in ans:
        print(i[0], end=' ')
        print(i[1])

Solution (Python):
n,k=map(int,input().split())
ans=set()
for i in range(2,n+1):
    ans.add((1,i))
c=0
import itertools
for x,y in itertools.combinations(list(range(2,n+1)),2):
    if c>=(n-1)*(n-2)//2-k:
        break
    c+=1
    ans.add((x,y))

if (n-1)*(n-2)//2-k<0:
    print(-1)
    exit()
print(len(ans))
for i,j in ans:
    print(i,j)

Solution (Python):
n, k = [int(x) for x in input().strip().split(" ")]
m=~-n*(n-2)//2
if k>m:
    print(-1)
else:
    m-=k
    print(m+n-1)
    for i in range(1, n):
        print(i, n)
    for i in range(1, n):
        for j in range(i+1, n):
            if m==0:
                break
            print(i, j)
            m-=1

Solution (Python):
def solve(string):
    n, k = map(int, string.split())
    c = (n - 1) * (n - 2) // 2 - k
    if c < 0:
        return "-1"
    ans = [str(n - 1 + c)]
    for i in range(1, n):
        ans.append("{} {}".format(i, n))
    for i in range(1, n):
        for j in range(i + 1, n):
            if c == 0:
                break
            c -= 1
            ans.append("{} {}".format(i, j))
    return "\n".join(ans)


if __name__ == '__main__':
    print(solve(input()))

Solution (Python):
def solve(string):
    n, k = map(int, string.split())
    c = (n - 1) * (n - 2) // 2 - k
    if c < 0:
        return "-1"
    ans = []
    for i in range(1, n):
        ans.append("{} {}".format(i, n))
    for i in range(1, n):
        for j in range(i + 1, n):
            if c == 0:
                break
            c -= 1
            ans.append("{} {}".format(i, j))
    return "{}\n".format(len(ans)) + "\n".join(ans)


if __name__ == '__main__':
    print(solve(input()))

Solution (Python):
def solve(string):
    n, k = map(int, string.split())
    c = (n - 1) * (n - 2) // 2
    if k > c:
        return "-1"
    ans = []
    for i in range(1, n):
        ans.append("{} {}".format(i, n))
    for i in range(1, n):
        for j in range(i + 1, n):
            if c - k == 0:
                break
            c -= 1
            ans.append("{} {}".format(i, j))
    return "{}\n".format(len(ans)) + "\n".join(ans)


if __name__ == '__main__':
    print(solve(input()))

Solution (Python):
def solve(string):
    n, k = map(int, string.split())
    if k > (n - 1) * (n - 2) // 2:
        return "-1"
    ans = []
    c = (n - 1) * (n - 2) // 2
    for i in range(1, n):
        ans.append("{} {}".format(i, n))
    for i in range(1, n):
        for j in range(i + 1, n):
            if c - k == 0:
                break
            c -= 1
            ans.append("{} {}".format(i, j))
    return "{}\n".format(len(ans)) + "\n".join(ans)


if __name__ == '__main__':
    print(solve(input()))

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


def main():
    n, k = LI()
    if k > (n - 1) * (n - 2) // 2:
        print(-1)
        return
    else:
        add_v = (n - 1) * (n - 2) // 2 - k
        print(n - 1 + add_v)
        cnt = 0
        for i in range(2, n + 1):
            print(1, i)
        if cnt == add_v:
            return
        for j in range(2, n + 1):
            for l in range(j + 1, n + 1):
                print(j, l)
                cnt += 1
                if cnt >= add_v:
                    return


main()

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

if K > L:
    print(-1)
else:
    Ms = []
    for i in range(2, N+1):
        Ms.append((1, i))
    c = L
    for i in range(2, N):
        if c <= K:
            break
        for j in range(i+1, N+1):
            Ms.append((i, j))
            c -= 1
            if c <= K:
                break

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

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

edge = [[] for i in range(n)]
edge[0] = list(range(1, n))
for i in range(1, n):
    for j in range(i+1, n):
        if r == 0:
            break
        edge[i].append(j)
        r -= 1

m = sum(len(i) for i in edge)
print(m)
for i in range(n):
    for j in edge[i]:
        print(i+1, j+1)

Solution (Python):
n,k=map(int,input().split())
def ans():
  m=n
  if k>((n-1)*(n-2)//2):
    print(-1)
    return
  else:
    c=(n-1)*(n-2)//2-k
    a=[]
    for i in range(2,n+1):
      a.append([1,i])
    if c>0:
      for i in range(2,n+1):
        for j in range(i+1,n+1):
          a.append([i,j])
          c-=1
          if c==0:
            break
        if c==0:
          break
    print(len(a))
    for i in range(len(a)):
      print(a[i][0],a[i][1])
ans()

Solution (Python):
from sys import stdin

N, K = [int(x) for x in stdin.readline().rstrip().split()]

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

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

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

for j in range(2, N + 1):
    if E == 0:
        break
    for k in range(j + 1, N + 1):
        ans.append((j, k))
        E -= 1
        if E == 0:
            break

print(N - 1 + ((N - 1) * (N - 2)) // 2 - K)
for a in ans:
    print(*a)

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

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

    tmp = ((N-1)*(N-2)) // 2
    if K > tmp:
        print(-1)
        return
    ans = [(1, i) for i in range(2, N+1)]
    count = tmp - K
    if count > 0:
        for i in range(2, N+1):
            for j in range(i+1, N+1):
                ans.append((i, j))
                count -= 1
                if count == 0:
                    break
            if count == 0:
                break
    
    print(len(ans))
    for i in range(len(ans)):
        print(ans[i][0], ans[i][1])


if __name__ == "__main__":
    main()

Solution (Python):
import sys


n, k = map(int, sys.stdin.readline().split())

def solve(n, k):
	mx = (n-1)*(n-2)//2
	isolated = [(i, j) for i in range(2, n) for j in range(i+1, n+1)]
	edges = [(1, j) for j in range(2, n+1)]
	while mx != k:
		edge = isolated.pop()
		edges.append(edge)
		mx -= 1
	return edges

if k > (n-1)*(n-2)//2:
	print(-1)
else:
	res = solve(n, k)
	print(len(res))
	for edge in res:
		print(*edge)

Solution (Python):
from collections import deque
n, k = map(int, input().split())
edges = deque()

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

for i in range(1, n):
    for j in range(i+1, n+1):
        edges.append((i, j))
m = int((n-1) + (n-1)*(n-2)//2 - k)
print(m)
for i in range(m):
    edge = edges.popleft()
    print(*edge)

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

l,r = 1,2

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

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

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

Solution (Python):
N,K=(int(x) for x in input().split(" "))
def comb(n):
    return int(n*(n-1)/2)
limit=comb(N-1)
max=comb(N)

if K>limit:
    print (-1)
else:
    print (max-K)
    a=0
    for i in range (1,N+1):
        for j in range (i+1,N+1):
            if a<max-K:
                a+=1
            else:
                break
            c=str(i)
            d=str(j)
            print (c+" "+d)
        if a>=max-K:
            break

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



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

G = []



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




count = 0
u = 1
v = 2
while (count < (N-1)*(N-2)//2 - K):
  G.append([u,v])
  v += 1
  if (v == N):
    u += 1
    v = u + 1
  count += 1

print(len(G))
for g in G:
  print(*g)

Solution (Python):
N, K = map(int, input().split())
d = (N-1)*(N-2)//2 - K
if d < 0:
    print(-1)
    exit()

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

Solution (Python):
import sys

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

if k > (n - 1) * (n - 2) // 2:
	print(-1)
else:
	rest = (n - 1) * (n - 2) // 2 - k
	print((n - 1) + rest)
	for i in range(2, n + 1):
		print("1 " + str(i))

	for i in range(2, n + 1):
		for j in range(i + 1, n + 1):
			if rest == 0:
				sys.exit()
			print(str(i) + " " + str(j))
			rest -= 1

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

Solution (Python):
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 i in range(2, N + 1):
    for j in range(i + 1, N + 1):
      if M == 0:
        break
      print(i, j)
      M -= 1
    if M == 0:
      break

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

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

if x<0:
  print(-1)
else:
  print(n-1+x)
  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 x == 0:
        exit()
      print(i,j)
      x -= 1

Solution (Python):

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, sep=" ")

c = 0
for i in range(2, N):
    for j in range(i + 1, N + 1):
        if (N - 1) * (N - 2) // 2 - K == c:
            exit()
        print(i, j, sep=" ")
        c += 1

Solution (Python):
N,K = map(int, input().split())
if K > (N*(N-1)//2)-(N-1):
    print(-1)
    exit()
else:
    hen = [[0,0] for _ in range((N-1)+((N-1)*(N-2)//2)-K)]
    for i in range(2,N+1):
        hen[i-2] = [1,i]
    tmp = 1
    br = 0
    if hen[-1] !=[0,0]:
        print(len(hen))
        for l in hen:
            print(*l)
        exit()
    for j in range(2,N):
        for k in range(j+1,N+1):
            hen[N-2+tmp] = [j,k]
            tmp += 1
            if N-2+tmp == len(hen):
                br = 1
                break
        if br == 1:
            break
    print(len(hen))
    for l in hen:
        print(*l)

Solution (Python):

n,k=map(int,input().split())
import sys
if k>(n-1)*(n-2)//2:
	print(-1)
	sys.exit()
res=[[1,i+1] for i in range(1,n)]
c=0
for i in range(1,n-1):
	for j in range(i+1,n):
		if c<(n-1)*(n-2)//2-k:
			res.append([i+1,j+1])
		c+=1
print(len(res))
for t in res:
	print(" ".join(map(str,t)))

Solution (Python):

n,k=map(int,input().split())
import sys
if k>(n-1)*(n-2)//2:
	print(-1)
	sys.exit()
res=[[1,i+1] for i in range(1,n)]
c=0
for i in range(1,n-1):
	for j in range(i+1,n):
		if c<(n-1)*(n-2)//2-k:
			res.append([i+1,j+1])
		c+=1
print(len(res))
for t in res:
	print(" ".join(map(str,t)))

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

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

P = (N - 1) * (N - 2) // 2
k = 2
while P > K:
    for i in range(k + 1, N + 1):
        ans.append((k, i))
        P -= 1
        if P == K:
            break
    k += 1

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

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
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
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)
    quit()
print((n-1)*(n-2)//2 + (n-1) - k)
for i in range(n-1):
    print(1,i+2)

a,b = 2,3
for _ in range((n-1)*(n-2)//2 - k):
    print(a,b)
    b += 1
    if b > n:
        a += 1
        b = a+1

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
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
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)
    quit()
print((n-1)*(n-2)//2 + (n-1) - k)
for i in range(n-1):
    print(1,i+2)

a,b = 2,3
for _ in range((n-1)*(n-2)//2 - k):
    print(a,b)
    b += 1
    if b > n:
        a += 1
        b = a+1

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

if ((N-2) * (N-1))//2 < K:
    print(-1)
else:
    M = N-1
    node = []
    for i in range(N-1):
        node.append([1,i+2])
    count1 = 2
    count2 = 3
    for j in range(((N-2) * (N-1))//2 - K):
        node.append([count1,count2])
        count2 += 1
        if count2 == N+1:
            count1 += 1
            count2 = count1+1
        M += 1
    print(M)
    for i in range(M):
        print("{} {}".format(node[i][0],node[i][1]))

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

edge = [[0] * N for _ in range(N)] 



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

def output(lst): 
    count = 0
    for i in lst:
        for j in i:
            if j == 1:
                count += 1
    print (count)
    for i in range(N):
        for j in range(N):
            if lst[i][j] == 1:
                if i <= j:
                    print (i + 1, j + 1)
                else:
                    print (j + 1, i + 1)
    return

for i in range(1, N): 
    edge[0][i] = 1
    
res = ((N - 1) * (N - 2)) //2 - K

if res == 0:
    output(edge)
    exit()

tmp = 0
while True:
    tmp += 1
    for i in range(1, N):
        if res == 0:
            
            output(edge)
            exit()
        if i + tmp < N:
            if i != i + tmp and edge[i][i + tmp] == 0 and edge[i + tmp][i] == 0:
                edge[i][i + tmp] = 1
                res -= 1
        else:
            if i != ((i + tmp) % N) + 1 and edge[i][((i + tmp) % N) + 1] == 0 and edge[((i + tmp) % N) + 1][i] == 0:
                edge[i][((i + tmp) % N) + 1] = 1
                res -= 1

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
























































edge = [[0] * N for _ in range(N)] 



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

def output(lst): 
    count = 0
    for i in lst:
        for j in i:
            if j == 1:
                count += 1
    print (count)
    for i in range(N):
        for j in range(N):
            if lst[i][j] == 1:
                if i <= j:
                    print (i + 1, j + 1)
                else:
                    print (j + 1, i + 1)
    return

for i in range(1, N): 
    edge[0][i] = 1
    
res = ((N - 1) * (N - 2)) //2 - K

if res == 0:
    output(edge)
    exit()

tmp = 0
while True:
    tmp += 1
    for i in range(1, N):
        if res == 0:
            
            output(edge)
            exit()
        if i + tmp < N:
            if i != i + tmp and edge[i][i + tmp] == 0 and edge[i + tmp][i] == 0:
                edge[i][i + tmp] = 1
                res -= 1
        else:
            if i != ((i + tmp) % N) + 1 and edge[i][((i + tmp) % N) + 1] == 0 and edge[((i + tmp) % N) + 1][i] == 0:
                edge[i][((i + tmp) % N) + 1] = 1
                res -= 1

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

if N == 2:
    if K == 0:
        print (1)
        print (1, 2)
        exit()
    else:
        print (-1)
        exit()

if N == 3:
    if K == 0:
        print (3)
        print (1, 2)
        print (2, 3)
        print (1, 3)
        exit()
    elif K == 1:
        print(2)
        print (1, 2)
        print (2, 3)
        exit()
    else:
        print (-1)
        exit()

if N == 4:
    if K == 0:
        print (6)
        for i in range(1, 5):
            for j in range(i + 1, 5):
                print (i, j)
        exit()
    elif K == 1:
        print (5)
        for i in range(1, 5):
            for j in range(i + 1, 5):
                if i == 3 and j == 4:
                    pass
                else:
                    print (i, j)
        exit()
    elif K == 2:
        print (3)
        for i in range(1, 4):
            print (i, i + 1)
        exit()
    elif K == 3:
        print (3)
        for i in range(2, 5):
            print (1, i)
        exit()
    else:
        print (-1)
        exit()

edge = [[0] * N for _ in range(N)] 



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

def output(lst): 
    count = 0
    for i in lst:
        for j in i:
            if j == 1:
                count += 1
    print (count)
    for i in range(N):
        for j in range(N):
            if lst[i][j] == 1:
                if i <= j:
                    print (i + 1, j + 1)
                else:
                    print (j + 1, i + 1)
    return

for i in range(1, N): 
    edge[0][i] = 1
    
res = ((N - 1) * (N - 2)) //2 - K

if res == 0:
    output(edge)
    exit()

tmp = 0
while True:
    tmp += 1
    for i in range(1, N):
        if res == 0:
            
            output(edge)
            exit()
        if i + tmp < N:
            if i != i + tmp and edge[i][i + tmp] == 0 and edge[i + tmp][i] == 0:
                edge[i][i + tmp] = 1
                res -= 1
        else:
            if i != ((i + tmp) % N) + 1 and edge[i][((i + tmp) % N) + 1] == 0 and edge[((i + tmp) % N) + 1][i] == 0:
                edge[i][((i + tmp) % N) + 1] = 1
                res -= 1

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

if N == 2:
    if K == 0:
        print (1)
        print (1, 2)
        exit()
    else:
        print (-1)
        exit()

if N == 3:
    if K == 0:
        print (3)
        print (1, 2)
        print (2, 3)
        print (1, 3)
        exit()
    elif K == 1:
        print(2)
        print (1, 2)
        print (2, 3)
        exit()
    else:
        print (-1)
        exit()

if N == 4:
    if K == 0:
        print (6)
        for i in range(1, 5):
            for j in range(i + 1, 5):
                print (i, j)
        exit()
    elif K == 1:
        print (5)
        for i in range(1, 5):
            for j in range(i + 1, 5):
                if i == 3 and j == 4:
                    pass
                else:
                    print (i, j)
        exit()
    elif K == 2:
        print (3)
        for i in range(1, 4):
            print (i, i + 1)
        exit()
    elif K == 3:
        print (3)
        for i in range(2, 5):
            print (1, i)
        exit()
    else:
        print (-1)
        exit()

edge = [[0] * N for _ in range(N)] 



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

def output(lst): 
    count = 0
    for i in lst:
        for j in i:
            if j == 1:
                count += 1
    print (count)
    for i in range(N):
        for j in range(N):
            if lst[i][j] == 1:
                if i <= j:
                    print (i + 1, j + 1)
                else:
                    print (j + 1, i + 1)
    return

if N >= 5:
    if False:
        for i in range(N): 
            edge[i][(i+1)%N] = 1
        for i in range(N - K):
            edge[i][(i+2)%N] = 1
        output(edge)
        
        exit()
    else: 
        for i in range(1, N): 
            edge[0][i] = 1
        
        res = ((N - 1) * (N - 2)) //2 - K
        
        if res > 0:
            flag = True
        else:
            flag = False
        
        tmp = 0
        while flag:
            tmp += 1
            for i in range(1, N):
                if res == 0:
                    flag = False
                    output(edge)
                    exit()
                if i + tmp < N:
                    if i != i + tmp and edge[i][i + tmp] == 0 and edge[i + tmp][i] == 0:
                        edge[i][i + tmp] = 1
                        res -= 1
                else:
                    if i != ((i + tmp) % N) + 1 and edge[i][((i + tmp) % N) + 1] == 0 and edge[((i + tmp) % N) + 1][i] == 0:
                        edge[i][((i + tmp) % N) + 1] = 1
                        res -= 1
        output(edge)
        exit()

Solution (Python):
n, k = map(int, input().split())
c = (n-1) * (n-2) // 2
if k > c:
    print(-1)
    exit()
m = n - 1 + c - k
print(m)
for i in range(2, n+1):
    print("1 {}".format(i))
i = 2
j = 3
while c > k:
    print("{} {}".format(i, j))
    if j < n:
        j += 1
    else:
        i += 1
        j = i + 1
    c -= 1