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


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N,K=MAP()

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

if K>(N-1)*(N-2)//2:
    print(-1)
    exit()
else:
    cnt=(N-1)*(N-2)//2
    for i in range(1, N):
        for j in range(i+1, N):
            if cnt==K: break
            edges.append((i+1, j+1))
            cnt-=1

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

Solution (Python):
n, k = map(int, input().split())
maxPossible = (n - 2) * (n - 1) // 2
if k > maxPossible:
    print(-1)
    exit()
ret = []
for i in range(2, n + 1):
    ret.append((1, i))
add = []
for i in range(2, n):
    for j in range(i + 1, n + 1):
        add.append((i, j))
for i in range(maxPossible - k):
    ret.append(add[i])
print(len(ret))
print('\n'.join('%d %d' % (u, v) for u, v in ret))

Solution (Python):
from math import factorial
n,k=[int(_) for _ in input().split()]
if n == 2 and k == 1:
    print(-1)
elif n == 2 and k == 0:
    print(1)
    print(1,2)
else:
    max = factorial(n-1)//factorial(n-3)//2
    if k > max:
        print(-1)
    else:
        cnt=max-k
        print(n+cnt-1)
        for i in range(2,n+1):
            print(1,i)
        nn=2
        while cnt > 0:
            print((nn%(n-1)+2),((nn%(n-1)+2)+((nn-2)//(n-1)-1))%(n-1)+2)
            nn+=1
            cnt-=1

Solution (Python):
n,k=map(int,input().split())
if k>(n-1)*(n-2)//2:
  print(-1)
else:
  Chk=[[False]*n for _ in range(n)]
  E=[]
  for i in range(2,n+1):
    E.append([1,i])
    Chk[0][i-1]=True
  res=(n-1)*(n-2)//2-k
  
  while res>0:
    for i in range(2,n+1):
      for j in range(i+1,n+1):
        if Chk[i-1][j-1]==False:
          s,g=i,j
          Chk[i-1][j-1]=True
          break
      else:
        continue
      break
    E.append([s,g])
    res-=1
  print(len(E))
  for s,g in E:
    print(s,g)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

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

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

    V = []

    import itertools

    
    for i in range(1, N):
        V.append((i, N))
    
    
    
    left = ((N-1) *  (N-2)  // 2) - K
    for i,j in itertools.combinations(range(1, N), 2):
        if left == 0: break
        V.append((i,j))
        left -= 1

    print(len(V))
    for item in V:
        print(item[0], item[1])
    return

main()

Solution (Python):
n,k=map(int,input().split())
if k>(n-1)*(n-2)//2:
  print(-1)
else:
  Chk=[[False]*n for _ in range(n)]
  E=[]
  for i in range(2,n+1):
    E.append([1,i])
    Chk[0][i-1]=True
  res=(n-1)*(n-2)//2-k
  
  while res>0:
    for i in range(2,n+1):
      for j in range(i+1,n+1):
        if Chk[i-1][j-1]==False:
          s,g=i,j
          Chk[i-1][j-1]=True
          break
      else:
        continue
      break
    E.append([s,g])
    res-=1
  print(len(E))
  for s,g in E:
    print(s,g)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)


debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)

def print_ans(edge):
    print(len(edge))
    for e in edge:
        e0 = e[0] + 1
        e1 = e[1] + 1
        print(e0, e1)

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

    node = [i for i in range(N)]
    edge = []

    for i in range(1, N):
        edge.append((0, i))

    dprint(edge)

    count = 0
    for i in range(1, N):
        for j in range(i+1, N):
            if nm1c2 - K - count == 0:
                print_ans(edge)
                return
            else:
                edge.append((i, j))
                count+= 1
    print_ans(edge)

solve()

Solution (Python):
n, k= map(int,input().split())
if k > (n-1)*(n-2)/2:
    print(-1)
elif n == 2:
    if k == 0:
        print(1)
        print(1,2)
    else:
        print(-1)
else:
    l = []
    for i in range(2,n+1):
        l.append([1,i])
    ima = (n-1)*(n-2) / 2
    
    hajime = 2
    owari = 3
    while(ima > k):
        l.append([hajime ,owari])
        ima -= 1
        if(owari < n):
            owari += 1
        else:
            hajime += 1
            owari = hajime + 1
    print(len(l))
    for i,j in l:
        print(i,j)

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

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

print(N-1+M-K)
for i in range(2,N+1):
    print(1,i)
s = 2
d = 3
for i in range(M-K):
    print(s,d)
    d+=1
    if d > N:
        s+=1
        d = s+1

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


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

remain = max_edges - K
print(N - 1 + remain)
for i in range(2, N + 1):
    print(1, i)

for i in range(2, N + 1):
    if remain <= 0:
        break
    for j in range(i + 1, N + 1):
        if remain <= 0:
            break
        print(i, j)
        remain -= 1

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

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

print(n-1+m-k)
for i in range(1, n):
    print(str(1) + ' ' + str(i+1))

s = 2
d = 3
for i in range(m-k):
    print(str(s) + ' ' + str(d))
    d += 1
    if d > n:
        s += 1
        d = s+1

Solution (Python):
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

INF = 10 ** 10




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




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

if K > maxn:
    print(-1)
elif N == 2:
    if K == 0:
        print(1)
        print(1,2)
    else:
        print(-1)
else:
    res = []
    for i in range(2, N+1):
        res.append([1, i])

    for i in range(2, N+1):
        for j in range(i+1, N+1):
            if i == j:
                continue
            if maxn > K:
                res.append([i, j])
                maxn -= 1
            if maxn == K:
                print(len(res))
                for u, v in res:
                    print(u, v)
                exit()

Solution (Python):


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

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

def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

if N == 2 and K == 0:
    print(1)
    print("1 2")
elif N == 2 and K == 1:
    print(-1)
elif comb(N-1,2) < K:
    print(-1)
else:
    maxnum = comb(N-1,2)
    for i in range(1,N):
        for j in range(1,N):
            if i < j:
                if maxnum != K:
                    link[i].append(j)
                    link[j].append(i)
                    maxnum -= 1
                else:
                    continue
    print(sum([len(l) for l in link])//2)
    for i in range(N):
        for j in link[i]:
            if i < j:
                print(i+1, j+1)

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

M = (N - 1) * (N - 2) // 2 - K
if M < 0:
    print(-1)
    quit()

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

u = 2
v = 1
for _ in range(M):
    if u + v > N:
        u += 1
        v = 1
    ans.append((u, u + v))
    v += 1

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

Solution (Python):
N, K = [int(n) for n in input().split()]
M = N*(N - 1)//2

if K > M - N + 1:
    print(-1)
else:
    print(M - K)
    for i in range(1, N):
        print(str(N) + " " + str(i))
    C = M - K - N + 1
    C_ = 0
    for i in range(1, N - 1):
        for j in range(i + 1, N):
            C_ += 1
            if C_ <= C:
                print(str(i) + " " + str(j))

Solution (Python):
def sol_act(n,k):
    from itertools import combinations
    if k>(n-1)*(n-2)//2:
        return -1
    res=set((i,n) for i in range(1,n))
    for _,c in zip(range((n-1)*(n-2)//2-k),combinations(range(1,n),2)):
        res.add(c)
    return res
def sol(inp=input):
    n,k=map(int,inp().split())
    res=sol_act(n,k)
    if res==-1:
        return('-1')
    return '\n'.join([str(len(res))]+['{} {}'.format(*l) for l in res])
  
print(sol())

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

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

if tmp != K:
    for i in range(2, N + 1):
        for j in range(i + 1, N+ 1):
            if i == j:
                continue
            else:
                ans.append((i, j))
                tmp -= 1
                if tmp == K:
                    break
        if tmp == K:
            break
if tmp == K:
    print(len(ans))
    for a in ans:
        print(a[0], a[1])
else:
    print(-1)

Solution (Python):
N,K = map(int,input().split())
Ni = (N-1)*(N-2)//2
if Ni < K:
    print(-1)
    exit()
M = N-1+Ni-K
print(M)
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 Ni-K <= 0:
            exit()
        print(i,j)
        Ni -= 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(N - 1):
		print(1, i + 2)
	s = 2
	t = 3
	for i in range(((N - 1) * (N - 2)) // 2 - K):
		if t <= N:
			print(s, t)
			t += 1
		else:
			s += 1
			t = s + 1
			print(s, t)
			t += 1

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

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

Solution (Python):
import sys
n, k = map(int, sys.stdin.readline().split())
if k > (n-1)*(n-2)//2:
    print(-1)
    quit()
t = (n-1)*(n-2)//2 - k
print(n-1+t)
for i in range(2, n+1):
    sys.stdout.write("{} {}\n".format(1, i))
for i in range(2, n+1):
    for j in range(i+1, n+1):
        if t == 0:
            break
        sys.stdout.write("{} {}\n".format(i, j))
        t -= 1
    if t == 0:
        break

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def YesNo(x): return 'Yes' if x else 'No'
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

def main():
    N, K = LI()
    if K > (N - 1) * (N - 2) // 2:
        print(-1)
        return
    M = (N * (N - 1) // 2) - K
    edges = [[N, i] for i in range(1, N)]
    cnt = (N - 1) * (N - 2) // 2
    for i in range(1, N):
        if cnt == K:
            break
        for j in range(i + 1, N):
            edges.append([i, j])
            cnt -= 1
            if cnt == K:
                break
    print(M)
    for e in edges:
        print(*e)
    return

main()

Solution (Python):

import sys
n, k = map(int, input().split())
k_max = (n - 2) * (n - 1) // 2

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

m = n - 1 + k_max - k

print(m)
cnt = 0
exit_flag = False
for i in range(1, n):
    for j in range(i + 1, n + 1):
        print(i, j)
        cnt += 1
        if cnt == m:
            exit_flag = True
            break
    if exit_flag:
        break

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

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

if m<k:
    print(-1)
else:
    l=m-k
    line=[]
    for i in range(n-1):
        line.append((1,i+2))
    
    counter=0
    for j in range(2,n):
        if counter==l:
            break
        for i in range(j+1,n+1):
            line.append((j,i))
            counter+=1
            if counter==l:
                break
        if counter==l:
            break
    print(len(line))
    for i in range(len(line)):
        print(str(line[i][0])+' '+str(line[i][1]))

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

l = (N-1)*(N-2)//2
ans_l = []

if K>l:
    print(-1)
else:
    fixed = 2
    tmp = 3
    [ans_l.append([1,i]) for i in range(2,N+1)]

    for _ in range(l-K):
        ans_l.append([fixed,tmp])
        tmp += 1
        if tmp==N+1:
            fixed += 1
            tmp = fixed + 1
    print(N-1 + l-K)
    for a in ans_l:
        print(a[0],a[1])

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

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

m = 0
if k > c:
  print(-1)
else:
  m_max = n - 1 + (c - k)
  print(m_max)
  for i in range(1, n + 1):
    for j in range(i + 1, n + 1):
      if m < m_max:
        print(i, j)
        m += 1
      else:
        break

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K):
    if K > ((N-1)*(N-2))//2:
        print(-1)
        return
    
    m = []
    for i in range(2, N+1):
        m.append((1, i))
    
    k = ((N-1)*(N-2))//2 - K
    for i in range(2, N+1):
        if k == 0:
            break
        for j in range(i+1, N+1):
            k -= 1
            m.append((i, j))
            if k == 0:
                break
        

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


def main():
    N, K = read_int_n()
    (slv(N, K))


if __name__ == '__main__':
    main()

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

Solution (Python):
def num(n):
    return n*(n+1)//2

from sys import exit
N, K = [int(n) for n in input().split()]
if num(N-2) < K:
    print(-1)
    exit()

cnt = num(N-2) - K
print(N-1 + cnt)
for i in range(2, N+1):
    print(1, i)



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

Solution (Python):
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

n, k = (int(i) for i in input().split())

if n == 2:
    if k == 0:
        print(1)
        print(1, 2)
    else:
        print(-1)
else:
    total = cmb(n - 1, 2)
    
    if total < k:
        print(-1)
    else:
        dif = total - k

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

        p = 2
        c = 3
        while True:
            if dif == 0:
                break

            if c > n:
                p += 1
                c = p + 1
                print(p, c)
                c += 1
            else:
                print(p, c)
                c += 1

            dif -= 1

Solution (Python):


import itertools
from collections import Counter
from collections import defaultdict
import bisect
from heapq import heappush, heappop


def output(edges):
    print(len(edges))
    for s, e in edges:
        print('{} {}'.format(s, e))


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

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

    ma = (N - 1) * (N - 2) // 2
    
    if K > ma:
        print(-1)
        exit()
    rest = ma - K
    cand = []
    for i in range(2, N):
        for j in range(i + 1, N + 1):
            cand.append((i, j))

    
    while rest > 0:
        i, j = cand.pop()
        edges.append((i, j))
        rest -= 1

    output(edges)


if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
ans = []
rest = (N-1) * (N-2) / 2 - K
if rest < 0:
    print(-1)
else:
    ans.append([1, 2])
    tmp = 1
    for i in range(3, N+1):
        ans.append([1, i])

    i = 2
    while rest > 0:
        j = i + 1
        while True:
            ans.append([i, j])
            j += 1
            rest -= 1
            if rest == 0 or j > N:
                break
        i += 1

    print(len(ans))
    for i in range(len(ans)):
        print(ans[i][0], ans[i][1])

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

    edges = [(1, i) for i in range(2, N+1)]
    for j in range(2, N):
        for k in range(j+1, N+1):
            if num == K:
                break
            edges.append((j, k))
            num -= 1

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


if __name__ == '__main__':
    main()

Solution (Python):
def friendships(N: int, K: int)->list:
    MAX = (N-1)*(N-2)//2
    if K > MAX:
        return []

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

    u = 2
    v = 3
    for _ in range(K, MAX):
        res.append((u, v))
        if v == N:
            u += 1
            v = u + 1
        else:
            v += 1

    return res


if __name__ == "__main__":
    N, K = map(int, input().split())

    ans = friendships(N, K)
    if ans:
        print(len(ans))
        for u, v in ans:
            print(u, v)
    else:
        print(-1)

Solution (Python):
[N,K]=[int(_) for _ in input().split()]
if K>(N-1)*(N-2)//2:
    print(-1)
else:
    edges=[(1,n) for n in range(2,N+1)]
    edges2=[(i,j) for i in range(2,N+1) for j in range(2,N+1) if i<j]
    for i in range((N-1)*(N-2)//2-K):
        edges.append(edges2[i])
    print(N-1+(N-1)*(N-2)//2-K)
    for e in edges:
        print(e[0],e[1])

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n, k = li()

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

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

    surp = (n-1)*(n-2) // 2 - k
    for frm in range(1, n):
        for to in range(frm+1, n):
            if surp == 0:
                break

            edges.append((frm+1, to+1))
            surp -= 1


    print(len(edges))
    for ei, ej in edges:
        print(ei, ej)

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(2,N+1):
        print('{} {}'.format(1,i))
    rest = (N-1)*(N-2)//2-K
    for i in range(2,N+1):
        if rest==0:
            break
        for j in range(i+1,N+1):
            if rest==0:
                break
            print('{} {}'.format(i,j))
            rest -= 1

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

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

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

Solution (Python):
import itertools

def main():
    N, K = map(int, input().split())
    max_ = int((N - 1) * (N - 2) / 2)
    if K > max_:
        print(-1)
        return
    M = N - 1 + max_ - K
    print(M)
    for i in range(2, N + 1):
        print(1, i)
    for u, v in [(x[0], x[1]) for x in itertools.combinations(range(2, N + 1), 2)][:max_ - K]:
        print(u, v)

main()

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

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

  while True:
    for j in range(2,n+1):
      for i in range(j+1,n+1):
        print(j,i)
        num=num-1
        
        if num==0:
          exit()

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

Solution (Python):
import sys

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

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

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

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

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

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("1 {}".format(i + 2))
  M -= N - 1
  a = N
  b = N - 1
  while M > 0:
    print("{} {}".format(a, b))
    b -= 1
    if b == 1:
      a -= 1
      b = a - 1
    M -= 1

Solution (Python):
from itertools import combinations


def solve(n, k):
    max = (n - 1) * (n - 2) // 2
    if max < k:
        print('-1')
        return
    edge = (n * (n - 1)) // 2 - k
    print(edge)
    ind = 1
    for i, j in combinations([i + 1 for i in range(n)], 2):
        print(i, j)
        ind += 1
        if ind > edge:
            break


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

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

if K > (N-1)*(N-2)//2:
    print(-1)
else:
    edges.append((1,2))
    for i in range(3, N+1):
        edges.append((2, i))
    sa = (N-1)*(N-2)//2 - K
    for i in range(min(sa, N-2)):
        sa -= 1
        edges.append((1, i+3))
    if sa != 0:
        i = 3
        j = 4
        while sa > 0:
            while j <= N and sa > 0:
                edges.append((i, j))
                j += 1
                sa -= 1
            i += 1
            j = i + 1
        print(len(edges))
        for i in range(len(edges)):
            print(edges[i][0], edges[i][1])
    else:
        print(len(edges))
        for i in range(len(edges)):
            print(edges[i][0], edges[i][1])

Solution (Python):
from itertools import combinations

N, K = map(int, input().split())
K_max = (N-1)*(N-2)//2
if K not in range(K_max + 1):
    print(-1)
else:
    edges = list(combinations(range(1, N+1), 2))[:N*(N-1)//2 - K]
    print(len(edges))
    for a, b in edges:
        print(a, b)

Solution (Python):
import sys
import itertools

def main():
    n, k = [int(i) for i in sys.stdin.readline().strip().split()]
    max_k = (n-1) * (n-2) // 2
    if max_k < k:
        print('-1')
        return
    
    pairs = [(1, i) for i in range(2, n+1)]
    l = max_k
    for c in itertools.combinations(list(range(2, n+1)), 2):
        if l == k:
            break
        pairs.append(c)
        l -= 1
    print(len(pairs))
    for p in pairs:
        print(p[0], p[1])
    return


main()

Solution (Python):
import sys
import itertools

n, k = map(int, input().split())
comb = (n - 1) * (n - 2) // 2
if k > comb:
    print(-1)
    sys.exit()
s = set()
for i in range(2, n + 1):
    s.add((1, i))
count = 0
max_count = comb - k
for t in itertools.combinations(range(2, n + 1), 2):
    if count == max_count:
        break
    s.add(t)
    count += 1
print(len(s))
for i, j in s:
    print(i, j)

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


if __name__ == "__main__":
    main()

Solution (Python):
n, k = list(map(int, input().split()))
if k>(n-2)*(n-1)//2:
  print(-1)
  exit()
m = 0
edge = [[] for i in range(n)]
edge[0].append(1)
m += 1
for i in range(2, n):
  edge[1].append(i)
  m += 1
rem = (n-2)*(n-1)//2-k
for i in range(n):
  if rem==0:
    break
  if i==1:
    continue
  for j in range(i+1, n):
    if rem==0:
      break
    if j==1:
      continue
    edge[i].append(j)
    m += 1
    rem -= 1
print(m)
for i, row in enumerate(edge):
  for e in row:
    print(i+1, e+1)

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

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

s = N * (N - 1) // 2 - K
if s < N - 1:
    print(-1)
else:
    print(s)
    for idx, (i, j) in enumerate(e, 1):
        print(i, j)
        if idx == s:
            break

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




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

edges = []
for i in range(1, N+1):
  for j in range(i+1, N+1):
    edges.append((i, j))
edges.sort(key=lambda x: x[0])
print(M)
for f, t in edges[:M]:
  print(f, t)

Solution (Python):
def solve():
    N, K = map(int, input().split())
    if (N - 2)*(N - 1)//2 < K: print(-1)
    else:
        totalpath = (N-2)*(N-1)//2
        ans = []
        for i in range(N - 1):
            ans.append([i+1, N])

        next = [1, 2]
        while totalpath > K:
            ans.append([next[0], next[1]])
            if next == [N - 2, N - 1] and totalpath > K + 1:
                print(-1)
                break
            elif next[1] == N - 1:
                next = [next[0]+ 1, next[0] + 2]
            else: next[1] += 1
            totalpath -= 1
        else:
            print(len(ans))
            for edge in ans:
                print(" ".join(map(str, edge)))

    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
N, K = [int(x) for x in input().split()]
edges = []
cnt = sum(range(1, N-1))
for i in range(2, N+1):
    edges.append((1,i))
if (K > cnt):
    print(-1)
elif (K == cnt):
    print(len(edges))
    for edge in edges:
        print(edge[0], edge[1])
else:
    for i in range(2, N+1):
        for j in range(i+1, N+1):
            edges.append((i,j))
            cnt -= 1
            if (K == cnt):
                print(len(edges))
                for edge in edges:
                    print(edge[0], edge[1])
                exit()

Solution (Python):
import itertools
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))
L = (N-1)*(N-2)//2 - K
i = 0
if L > 0:
    for pair in itertools.combinations(range(2,N+1),2):
        ans.append(pair)
        i+=1
        if i >= L:
            break
print(len(ans))
for a in ans:
    x,y = a
    print(x,y)