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















def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

    n,k = map(int, input().split())
    pair = (n-1)*(n-2)//2
    res = []
    if pair < k:
        print(-1)
    else:
        for i in range(1, n-1):
            if pair == k:
                break
            for j in range(i+1, n):
                if pair == k:
                    break
                pair -= 1
                res.append([i, j])
        print(n-1+len(res))
        for i in range(1, n):
            print(i, n)
        for a, b in res:
            print(a, b)


if __name__ == '__main__':
    main()

Solution (Python):
import itertools

n,k = map(int, input().split())
if k > (n-1) * (n-2) // 2:
  print(-1)
  exit()
m = n * (n-1) // 2 - k
print(m)
for i in range(1,n):
  print(i,n)
add = (n-1) * (n-2) // 2 - k
for i in itertools.combinations(range(1,n),2):
  if not add:
    break
  print(*i)
  add -= 1

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

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

print(len(ans))
for value in ans:
  print(*value)

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

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

print(len(ans))
for value in ans:
  print(*value)

Solution (Python):


import sys



















def main():
    mod = 1000000007                  
    inf = float('inf')
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    n, k = mi()
    maximum_pair = (n - 1) * (n - 2) // 2
    if k > maximum_pair:
        print(-1)
    else:
        m = (n - 1) + (maximum_pair - k)
        print(m)
        for i in range(2, n + 1):
            print("1 {}".format(i))
        cnt = maximum_pair - k
        for u in range(2, n + 1):
            for v in range(u + 1, n + 1):
                if cnt > 0:
                    print("{} {}".format(u, v))
                    cnt -= 1
                else:
                    exit()


if __name__ == "__main__":
    main()

Solution (Python):



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

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

    print(M)
    for i in range(1, N):
        print(N, i)
    m = 0
    for i in range(1, N):
        for j in range(i + 1, N):
            if m < M2:
                print(i, j)
                m += 1
            else:
                break
        if m == M2:
            break

Solution (Python):
N, K = map(int, input().split())
mx = (N-1)*(N-2)/2
if mx < K:
  print(-1)
  exit()
pair = []
for i in range(N-1):
  pair += [[i+1, N]]
add = mx - K
edge = []
for i in range(N-1):
  for j in range(i):
    edge += [[i+1, j+1]]

for i in range(int(add)):
  pair += [edge[i]]

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

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,K=map(int,input().split())
    if (N-1)*(N-2)//2<K:
        print(-1)
    else:
        ans=[]
        for i in range(2,N+1):
            ans.append((1,i))
        i,j=2,3
        for _ in range((N-1)*(N-2)//2-K):
            ans.append((i,j))
            j+=1
            if j>N:
                i+=1
                j=i+1
        print(len(ans))
        for a in ans:
            print(*a)

if __name__ == '__main__':
    main()

Solution (Python):

import sys
input = sys.stdin.readline









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

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

cnt = (N-1)*(N-2)//2
if cnt < K:
    print(-1)
    sys.exit()

for i in range(N-1):
    for j in range(i+1,N-1):
        if cnt>K:
            Ans.append((i,j))
            cnt -= 1

print(len(Ans))
for u,v in Ans:
    print(u+1,v+1)

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

    print(len(edge))
    for i in range(0,len(edge)):
        print(*edge[i])

Solution (Python):
import sys

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

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

edges = [(1, v) for v in range(2, N+1)]

num = (N-1)*(N-2)//2
for u in range(2, N+1):
    for v in range(u+1, N+1):
        if num == K:
            break
        edges.append((u, v))
        num -= 1
    else:
        continue
    break

print(len(edges))
print('\n'.join(map(lambda x: str(x[0])+' '+str(x[1]), edges)))

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

if k > (n-1) * (n-2) // 2:
    print(-1)
else:
    print((n-1) * n // 2 - k)
    count = 0
    for i in range(1,n+1):
        for j in range(i+1,n+1):
            print(i,j)
            count += 1
            if count == (n-1) * n // 2 - k:
                exit()

Solution (Python):


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

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

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

combs = []
for i in range(n - 1):
    for j in range(i):
        combs.append((j, i))

edges.extend(combs[:lim - k])

print(len(edges))
for a, b in edges:
    print(a + 1, b + 1)

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

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([1,i+2])

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

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

    print(len(ans))
    for i in range(len(ans)):
        print(*ans[i])

Solution (Python):


def main():
    n, k = map(int, input().split())
    kmax = ((n - 1) * (n - 2)) // 2
    if k > kmax:
        print(-1)
        return

    edges = getedges(n, k, kmax)
    print(len(edges))
    for i, j in edges:
        print(i, j)

def getedges(n, k, kmax):
    edges = []
    for i in range(1, n):
        edges.append((i, n))
    if kmax == k:
        return edges
    c = 0
    for i in range(1, n - 1):
        for j in range(i + 1, n):
            edges.append((i, j))
            c += 1
            if c == kmax - k:
                return edges
    assert False

if __name__ == "__main__":
    main()

Solution (Python):

"""
問題：
頂点数n,辺数mとして最短距離が2となる辺がk個となる
重みなし無向グラフが存在するかを調べる。
解法：
まず、うにグラフの状態がkの最大値であることが言える。
(うにグラフ…頂点1に全ての頂点を繋いだ状態。n-1辺になる。)
この時のkは2頂点の組み合わせ数(n-1)C2なので、これを超えるkは
満たせず、そうでなければ適当な2頂点を繋げばそれ以下の値を全て
満たせることになる。(二頂点を繋いでも他の辺の最短距離に影響しない)
"""
import sys
import itertools
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

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


def large_conbination(n,k): 
    res1 = 1
    for i in range(n,n-k,-1):
        res1*=i
    res2 = 1
    for i in range(1,k+1):
        res2*= i
    return res1//res2

mx = large_conbination(n-1,2) 

if k > mx:
    print(-1)
else:
    graph = []
    for i in range(1,n):
        graph.append((1,i+1)) 
    count = 0
    for i in itertools.combinations([i for i in range(2,n+1)],2):
        if k == mx-count:
            break
        graph.append(i)
        count += 1
    print(len(graph))
    for i,j in graph:
        print(i,j)

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

Solution (Python):


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

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

    xx2 = xx2[:((N-1)*(N-2))//2-K]

    ans = XX+xx2
    print(len(ans))

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



if __name__ == "__main__":
    resolve()

Solution (Python):
import sys

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

def main():
  m = n - 1
  cnt = (n - 1) * (n - 2) // 2
  if k > cnt: print(-1); return
  edges = [(1, v) for v in range(2, n + 1)]

  for u in range(2, n):
    for v in range(u + 1, n + 1):
      if cnt == k: break
      edges.append((u, v))
      m += 1
      cnt -= 1

  print(m)
  for u, v in edges:
    print(u, v)

if __name__ == '__main__':
  main()

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


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

Solution (Python):
n,k=map(int,input().split())
if k>(n-2)*(n-1)//2:
	print(-1)
	exit()
elif k==(n-2)*(n-1)//2:
	print(n-1)
	for i in range(n-1):
		print(1,i+2)
	exit()
ans=[]
for i in range(n-1):
	ans.append((1,i+2))
cnt=(n-2)*(n-1)//2
a=2
b=3
while cnt>k:
	cnt-=1
	ans.append((a,b))
	b+=1
	if b==n+1:
		b=a+2
		a+=1
print(len(ans))
for x in ans:
	print(*x)

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

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

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

print(n - 1 + maximum - k)

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

remainder = maximum - k
for i in range(2, n):
    for j in range(i+1, n+1):
        if remainder <= 0:
            exit()
        print(i, j)
        remainder -= 1

Solution (Python):
N, K = map(int, input().split())
if 2 * K > (N - 1) * (N - 2):
  print(-1)
else:
  M = N - 1
  D = {1: [_ for _ in range(2, N+1)]}
  cnt = ((N - 1) * (N - 2)) // 2
  pnt = 2
  qnt = 3
  while cnt != K:
    cnt -= 1
    if pnt in D:
      D[pnt].append(qnt)
    else:
      D[pnt] = [qnt]
    qnt += 1
    if qnt == N + 1:
      pnt += 1
      qnt = pnt + 1
    M += 1
  print(M)
  for i in D:
    for j in D[i]:
      print(i, j)

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

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

Solution (Python):
import itertools

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

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

if t < 0:
  print( -1 )

else:
  print( n - 1 + t )
  
  for i in range(1, n):
    print(1, i+1)
  
  for j in range(1, n):
    for i in range(1, j): 
      if t == 0:
        break
      print(i+1,j+1)
      t -= 1

Solution (Python):
import itertools

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

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

if t < 0:
  print( -1 )

else:
  print( n - 1 + t )
  
  for i in range(1, n):
    print(1, i+1)
  
  for i,j in itertools.combinations(range(1,n),2):
    if t == 0:
      break
    print(i+1,j+1)
    t -= 1

Solution (Python):
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)
    rem = (n-1)*(n-2)//2 - k
    cnt = 0
    for i in range(2, n+1):
        if cnt == rem:
            break
        for j in range(i+1, n+1):
            cnt += 1
            print(i, j)
            if cnt == rem:
                break

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

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

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

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

a = 2
d = 3
for i in range(b):
    print(a,d)
    if d == n:
        a += 1
        d = a+1
    else:
        d += 1

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

if K > (N-1)*(N-2) // 2:
    print(-1)
else:
    max_dif = (N-1)*(N-2) // 2 - K
    M = N - 1 + max_dif
    print(M)
    for i in range(N-1):
        print("{} {}".format(1, i+2))
    node1 = 2
    node2 = 3
    for i in range(max_dif):
        print("{} {}".format(node1, node2))
        if node2 == N:
            node1 += 1
            node2 = node1 + 1
        else:
            node2 += 1

Solution (Python):









import sys



ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, k = list(map(int, ipti().split()))

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

    if k_lim < k:
        print(-1)
        sys.exit()

    m1 = n - 1
    m2 = k_lim - k

    print(m1 + m2)

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

    e1 = 2
    e2 = 3
    e_num = 0

    while e_num < m2:
        print(e1, e2)
        e_num += 1
        e2 += 1
        if e2 > n:
            e1 += 1
            e2 = e1 + 1



if __name__ == '__main__':
    main()

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

Solution (Python):
def main():
    n, k = map(int, input().split())
    mx = (n-1)*(n-2)//2
    if mx < k:
        print(-1)
        return
    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])         
    m = len(ans)
    print(m)
    for i in range(m):
        print(*ans[i])

main()

Solution (Python):
n,k = map(int, input().split())
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)
def comb(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res
cm = comb(n-1,2)

if n==2 and k>=1:
    print(-1)
    exit()

if cm < k:
    print(-1)
    exit()

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

from itertools import *
cnt=0
for e in combinations(range(2,n+1),2):
    if cnt== cm-k:
        break
    cnt+=1
    ans.append([e[0],e[1]])

print(len(ans))
for a in ans:
    print(*a)

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

if k <= (n - 1) * (n - 2) // 2:
    g = [0] * (n * n)
    for i in range(n * n):
        u = i // n
        v = i % n
        if u < v:
            g[i] = 1

    i = 0
    for _ in range(k):
        while g[i] == 0 or i % n == n - 1:
            i += 1
        g[i] = 0

    cnt = sum(g)
    print(cnt)
    for i, e in enumerate(g):
        if e:
            u = i // n + 1
            v = i % n + 1
            print(u, v)

else:
    print(-1)

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





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






else:
    
    Cand = []
    for i in range(2, n):
        for j in range(i+1, n+1):
            Cand.append([str(i),str(j)])
    Ans = []
    for i in range(2,n+1):
        Ans.append([str(1),str(i)])
    for i in range(int((n-1)*(n-2)/2)-k):
        Ans.append(Cand[i])
    
    print(len(Ans))
    for i in range(len(Ans)):
        print(' '.join(Ans[i]))

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

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

ans = []

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

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

if k > dic2:
    print(-1)
    sys.exit()

a = 2
b = 3
while k < dic2:
    ans.append([a, b])
    dic2 -= 1
    b += 1
    if b > n:
        a += 1
        b = a + 1
    if a >= n and dic2 > k:
        print(-1)
        sys.exit()
print(len(ans))
for i in ans:
    print(*i)

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

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

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

for i in range(2,n+1):
    print(' '.join(map(str,[1,i])))

now = max_k
for i in range(2,n):
    for j in range(i+1,n+1):
        if(now == k):
            exit()
        print(' '.join(map(str,[i,j])))
        now -= 1

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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

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

if up < K:
    print(-1)
    exit()

res = []

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

K = up - K

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

    if K == 0:
        break

print(len(res))

for u, v in res:
    print(u, v)

Solution (Python):
import sys
input = sys.stdin.readline
INF = 10 ** 9
MOD = 10 ** 9 + 7

def main(): 
    n,k = map(int,input().split())
    
    if k > (n-1)*(n-2)//2:
        print(-1)
        exit()
    
    print(n*(n-1)//2 - k)
    for i in range(2,n+1):
        print(1,i)
    
    x = (n-1)*(n-2)//2 - k
    i = 2
    j = 3
    while x > 0:
        x -= 1
        print(i,j)
        j += 1
        if j == n + 1:
            i += 1
            j = i + 1
   
if __name__=='__main__':
    main()

Solution (Python):
import itertools
n, k = map(int,input().split())
a = [i for i in range(2,n+1)]
a_comb = list(itertools.combinations(a,2))
if k > (n-1)*(n-2) // 2:
    print(-1)
else:
    ans = []
    for i in range(2, n+1):
        ans.append((1, i))
    for i in range((n-1)*(n-2)//2 - k):
        ans.append((a_comb[i][0], a_comb[i][1]))
    print(len(ans))
    for i in range(len(ans)):
        print(ans[i][0], ans[i][1])

Solution (Python):
import sys
from itertools import combinations

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
limit = (N-1) * (N-2) // 2
if K > limit:
    print(-1); exit()

A = [(1, x) for x in range(2, N+1)]

for i, (x, y) in zip(range(limit-K), combinations(range(2, N+1), 2)):
    A.append((x, y))

print(len(A))
for a in A:
    print(*a)

Solution (Python):
n,k = map(int,input().split())
if k > n*(n-1)//2 - (n-1):
  print(-1)
  quit()
g = [[1,i+1] for i in range(1,n)]
t = (n-1)*(n-2)//2
for i in range(2,n):
  for j in range(i+1,n+1):
    if t == k:
      print(len(g))
      for l in g:
        print(*l)
      quit()
    g.append([i,j])
    t -= 1
print(len(g))
for l in g:
  print(*l)

Solution (Python):
import sys
def main():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    num = (N-1)*(N-2)//2
    if K > num:
        print(-1)
        return
    ans = []
    for i in range(2, N+1):
        ans.append((1, i))
    for i in range(2, N+1):
        for j in range(i+1, N+1):
            if num == K: break
            ans.append((i, j))
            num -= 1
    print(len(ans))
    for u, v in ans:
        print(u, v)

if __name__ == '__main__':
    main()

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

def main():
    max = (n-1)*(n-2)//2
    if max < k:
        print(-1)
        return 0
    add = max - k
    m = n-1 + add
    print(m)
    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 add <= 0:
                return 0
            print(i, j)
            add -= 1

main()

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

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

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

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

Solution (Python):

n,k=map(int,input().split())
edges=[[1,i] for i in range(2,n+1)]
cnt=(n-1)*(n-2)//2
ans=n-1
if cnt<k:
    print(-1)
else:
    for i in range(2,n):
        for j in range(i+1,n+1):
            if cnt>k:
                cnt-=1
                ans+=1
                edges.append([i,j])
            else:
                break
    print(ans)
    for edge in edges:
        print(*edge)

Solution (Python):
n, k = map(int, input().split())
if k > (n - 1) * (n - 2) / 2:
    print(-1)
else:
    
    ans = []
    appended_edges = set()
    for i in range(2,n+1):
        ans.append([1,i])
        appended_edges.add((1,i))
    
    flag = False
    
    count = (n-1)*(n-2) / 2
    for i in range(2,n+1):
        for j in range(i,n+1):
            if k == count:
                flag = True
                break
            if (i,j) not in appended_edges and i != j:
                ans.append([i,j])
                appended_edges.add((i,j))
                count -= 1
        if flag:
            break
    print(len(ans))
    for i in range(len(ans)):
        print("{} {}".format(ans[i][0], ans[i][1]))

Solution (Python):
N, K = map(int, input().split())
remaining = (N-1)*(N-2)//2 - K
if remaining < 0:
    print(-1)
    exit()
edges = []
for i in range(2, N+1):
    edges.append((1, i))
u = 2
v = 3
while remaining > 0:
    edges.append((u, v))
    v += 1
    if v == N+1:
        u += 1
        v = u + 1
    remaining -= 1
print(len(edges))
for u, v in edges:
    print(u, v)

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

def main():
    N,K = map(int,input().split())
    if K > (N-1)*(N-2)//2:
        print(-1)
    else:
        ans = [(1,i+2) for i in range(N-1)]
        rest = (N-1)*(N-2)//2-K
        l = [i+2 for i in range(N-1)]
        c = 0
        for v in itertools.combinations(l,2):
            if c == rest:
                break
            else:
                ans.append(v)
                c += 1
        print(len(ans))
        for a,b in ans:
            print(a,b)

if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n, k = map(int, input().split())
    if (n-1)*(n-2)//2 < k:
        print("-1")
    else:
        ans = []
        buf = (n-1)*(n-2)//2
        for i in range(2, n+1):
            ans.append([1, i])
        for i in range(2, n):
            if buf == k:
                break
            for j in range(i+1, n+1):
                ans.append([i, j])
                buf -= 1
                if buf == k:
                    break
        print(len(ans))
        for v in ans:
            print(v[0], v[1])

if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

n,k=map(int,input().split())
if (n-1)*(n-2)//2<k:
    print(-1)
    quit()
print(n-1+(n-1)*(n-2)//2-k)
for i in range(n-1):
    lst=[1,i+2]
    print(*lst)

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

for i in range((n-1)*(n-2)//2-k):
    print(*lst[i])

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

Solution (Python):
N, K = (int(i) for i in input().split())


ma = (N-2)*(N-1)//2
cou = 0
if N == 2:
    if K > 0:
        print("-1")
    else:
        print("1")
        print("1 2")
else:
    if K > ma:
        print("-1")
    else:
        print(N-1 + ma - K)
        for i in range(1, N):
            print("{} {}".format(i, N))
        for i in range(1, N):
            for j in range(1, N):
                if cou >= ma - K:
                    exit()
                if i < j:
                    print(i, j)
                    cou += 1

Solution (Python):
import os, sys, re, math, queue

N,K = list(map(int,input().split(' ')))
ans = []

if (N-1)*(N-2) < 2*K:
	print('-1')
	sys.exit()

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

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

limit = N - 1 + (N-1)*(N-2)//2 - K
print(limit)
for k in range(limit):
	print(ans[k])

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

k = (N-1)*(N-2) // 2
if k < K:
    print(-1)
else:
    d = k - K
    M = N-1 + d
    print(M)

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

    count = 0

    flag = True
    if d > 0:
        for i in range(N-2):
            for j in range(i+1, N-1):
                print(i+1, j+1)
                count += 1
                if count == d:
                    flag = False
                    break
            if flag:
                continue
            else:
                break

Solution (Python):
n, k = map(int, input().split())
m = (n - 1) * (n - 2) // 2
if k > m:
    print(-1)
    exit(0)
print(n + m - k - 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 == k:
            exit(0)
        print(i, j)
        k += 1

Solution (Python):
from itertools import combinations
n,k = map(int,input().split())
if k > (n-1)*(n-2)//2:
    print(-1)
else:
    count = n-1 + (n-1)*(n-2)//2 - k
    ans = [(1,i) for i in range(2,n+1)]
    plus = (n-1)*(n-2)//2 - k
    print(count)
    num = 0
    rem = [i for i in range(2,n+1)]
    for a,b in combinations(rem,2):
        if num == plus:
            break
        ans.append((a,b))
        num += 1
    for a,b in ans:
        print(a,b)

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

if k>(n-1)*(n-2)//2:
    print(-1)
else:
    c=(n-1)*(n-2)//2-k
    print(n-1+c)
    for i in range(1,n):
        print(i,n)
    for a,b in itertools.combinations(range(1,n),2):
        if c==0:
            break
        print(a,b)
        c-=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)
  
  cnt = (N-1)*(N-2)//2
  flag = False
  for i in range(N-2):
    for j in range(i+1,N-1):
      if cnt == K:
        flag=True
        break
      print(i+1,j+1)
      cnt -= 1
    if flag:
      break

Solution (Python):
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
from fractions import gcd
from copy import deepcopy
import math
import queue
import numpy as np

Mod = 1000000007
import sys
sys.setrecursionlimit(1000000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

def factorial(i):
    if i == 1:
        return 1
    else:
        return i*factorial(i-1)
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)




def main(): 
    n,k = map(int,input().split())
    mk = (n-1)*(n-2)//2
    if k > mk:
        print(-1)
    else:
        rest = mk - k
        print(n-1+rest)
        for i in range(n-1):
            print(1,i+2)
        t = [2,3]
        for i in range(rest):
            if t[1] <= n:
                print(t[0],t[1])
                t[1] += 1
            else:
                t = [t[0]+1,t[0]+2]
                print(t[0],t[1])
                t[1] += 1
if __name__ == "__main__":
    main()

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)
cnt=((n-1)*(n-2))//2-k
for i in range(2,n+1):
  for j in range(i+1,n+1):
    if cnt>0:
      print(i,j)
      cnt-=1
    else:
      break