Problem:
 Score: 500 points    Problem Statement  Does there exist an undirected graph with N vertices satisfying the following conditions?   The graph is simple and connected.  The vertices are numbered 1, 2, ..., N .  Let M be the number of edges in the graph. The edges are numbered 1, 2, ..., M , the length of each edge is 1 , and Edge i connects Vertex u_i and Vertex v_i .  There are exactly K pairs of vertices (i,\ j)\ (i < j) such that the shortest distance between them is 2 .   If there exists such a graph, construct an example.      Constraints  All values in input are integers.  2 \leq N \leq 100  0 \leq K \leq \frac{N(N - 1)}{2}         Input  Input is given from Standard Input in the following format:  N  K       Output If there does not exist an undirected graph with N vertices satisfying the conditions, print -1 .  If there exists such a graph, print an example in the following format (refer to Problem Statement for what the symbols stand for):  M  u_1  v_1  :  u_M  v_M   If there exist multiple graphs satisfying the conditions, any of them will be accepted.        Sample Input 1 5 3      Sample Output 1 5
4 3
1 2
3 1
4 5
2 3  This graph has three pairs of vertices such that the shortest distance between them is 2 : (1,\ 4) , (2,\ 4) , and (3,\ 5) . Thus, the condition is satisfied.       Sample Input 2 5 8      Sample Output 2 -1  There is no graph satisfying the conditions.

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

L = []
M = 0

for i in range(2, N + 1):
	L.append((1, i))
	M += 1
	
k = max_K

if K == k:
	print(M)
	for l in range(M):
		print(L[l][0], L[l][1])

for i in range(2, N):
	for j in range(i + 1, N + 1):
		L.append((i, j))
		k -= 1
		M += 1
		if K == k:
			print(M)
			for l in range(M):
				print(L[l][0], L[l][1])

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

Solution (Python):
def check(N, K):
    if K <= (N - 1)*(N-2) /2:
        k = int((N - 1)*(N-2)/2 - K)
        print(N-1 + k)
        for i in range(2, N+1):
            print(1, i)
        l = 2
        r = 3
        if k != 0:
            for i in range(k):
                if l > N or r > N:
                    break
                print(l, r)
                r+=1
                if r == N+1:
                    l+=1
                    r=l+1
        return
    else:
        print(-1)
        return


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

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

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

ans = ''
M = 0
for i in range(N-1):
    tmp = str(i+1) + ' ' + str(N) + '\n'
    ans += tmp
    M += 1
counter = (N-2) * (N-1) // 2

for i in range(N-1):
    for j in range(i+1, N-1):
        if counter > K:
            tmp = str(i+1) + ' ' + str(j+1) + '\n'
            ans += tmp
            M += 1
            counter -= 1

print(M)
print(ans)

Solution (Python):

"""
E - Friendships
https://atcoder.jp/contests/abc131/tasks/abc131_e

"""
import sys


from itertools import combinations

def solve(N, K):
    limit = (N-1) * (N - 2) // 2
    if K > limit:
        print(-1)
        return

    g = []
    for i in range(2, N+1):
        g.append('{} {}'.format(1, i))
    rem = limit - K
    for f, t in combinations(range(2, N+1), 2):
        if rem <= 0:
            break
        g.append('{} {}'.format(f, t))
        rem -= 1
    print(len(g))
    print(*g, sep='\n')


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


if __name__ == '__main__':
    main(sys.argv[1:])

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

if K > cmb:
    print('-1')
else:
    print(N-1+cmb-K)
    for i in range(N-1):
        print(1,i+2)
    cnt = 0
    i = 2
    j = 3
    while cnt < cmb-K:
        print(i,j)
        if j == N:
            i += 1
            j = i + 1
        else:
            j += 1
        cnt += 1

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

calc = (N*(N-3))//2 + 1

if calc < K :
    print(-1)
else :
    cnt = calc - K

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

    for i in range(2,N) :
        if not cnt > 0 :
            break

        for j in range(i+1,N+1) :
            if not cnt > 0 :
                break

            ans.append([i,j])
            cnt -= 1

    M = len(ans)

    print(M)
    for res in ans :
        print(*res)

Solution (Python):
from itertools import combinations, islice

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

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

if K <= M:
	C = (N - 1) + (M - K)
	print(C)
	for a, b in islice(combinations(range(1, N + 1), r=2), C):
		print(a, b)
else:
	print(-1)

Solution (Python):


from itertools import combinations
from collections import deque


def solve():
  uv = set()
  N,K = map(int, input().split())
  if N==2:
    if K==0:
     uv.add((1,2))
  elif N==3:
    if K==0:
      uv = set((1,2),(2,3),(1,3))
    elif K==1:
      uv = set((1,2),(2,3))
  elif N >= 4:
    if K <= (N-1)*(N-2)//2:
      for i in range(2,N+1):
        uv.add((1,i))
      combi = deque(combinations(range(2,N+1),2))
      K = (N-1)*(N-2)//2 - K
      while K > 0:
        p,q = combi.popleft()
        uv.add((p,q))
        K -= 1
  return uv


if __name__ == "__main__":
  uv = solve()
  if len(uv)==0:
    print(-1)
  else:
    print(len(uv))
    for u,v in uv:
      print(u,v)

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

M = N - 1
flg = 1
R = (N-1) * (N-2) // 2
edge_list = []
for i in range(2, N+1):
    edge_list.append([1, i])

if K > R:
    flg = -1
elif K == R:
    pass
else:
    for i in range(2, N):
        for j in range(i+1, N+1):
            edge_list.append([i, j])
            M += 1
            R -= 1
            if K == R:
                break
        if K == R:
            break

if flg == -1:
    print(-1)
else:
    print(M)
    for edge in edge_list:
        p, q = edge
        print(str(p), str(q))

Solution (Python):

"""
Created on Sun Jun 23 09:58:43 2019

@author: Yamazaki Kenichi
"""
import sys

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

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

if K > max_n:
    ans = -1
    print(ans)
    sys.exit()

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

link_n = max_n - K
counta = 0
for i in range(1,N):
    for j in range(i+1,N):
        if counta == link_n:
            break
        uv.append([i,j])
        counta += 1
    else:
        continue
    break

M = N - 1 + link_n
print(M)
for c in uv:
    print(c[0],c[1])

Solution (Python):

from math import factorial

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

def main():
    num, k = map(int, input().split())

    if num == 2:
        max_num = 10
    else:
        max_num = combinations_count(num - 1, 2)
    

    if k > max_num or num == 2:
        if num == 2 and k == 0:
            print(1)
            print(1, 2)
        else:
            print('-1')
    else:
        edge = num - 1
        now_count = max_num
        data = [[num, i] for i in range(1, num)]
        for i in range(1, num):
            for j in range(i + 1, num):
                if now_count > k:
                    edge += 1
                    now_count -= 1
                    data.append([i, j])
                else:
                    pass

        print(edge)
        for i, j in data:
            print(i, j)

if __name__ == '__main__':
    main()

Solution (Python):



def main():
    n, k = map(int, input().split())
    max_comb = (n - 1) * (n - 2) // 2

    if k > max_comb:
        print(-1)
        exit()

    ans = list()
    candidates = list()

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

    for i in range(1, n - 1):
        for j in range(i + 1, n):
            candidates.append((i + 1, j + 1))

    for i in range(max_comb - k):
        ans.append(candidates[i])

    m = len(ans)
    print(m)

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


if __name__ == '__main__':
    main()

Solution (Python):

import sys
def input(): return sys.stdin.readline().rstrip()
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
import heapq
INF = float("inf")
sys.setrecursionlimit(10**7)

dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W
def mi(): return map(int, input().split())
def ii(): return int(input())


N, K = mi()
maxK = (N-1)*(N-2)//2

if K>maxK:
  print(-1)
  exit()

edges = [] 



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



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


cnt = 0 
for i, j in combinations(range(2, N+1),2): 
  if cnt >= now_k_num - K: 
    break
  else:
    edges.append((i,j))
    cnt += 1

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

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

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

a = []  

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

x = (N-1)*(N-2)/2 - K
for i in range(2,N+1):
  for j in range(i+1,N+1):
    if x==0:
      break
    else:
      a.append([i,j])
      x = x-1
  
print(len(a))
for i in a:print(*i, sep=' ')

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

calc = (N-2) + sum([(N-i) for i in range(3,N)])

if calc < K :
    print(-1)

else :
    ans = []
    
    ans.append([1,2])
    
    for i in range(3,N+1) :
        ans.append([2,i])
    
    for i in range(3,N+1) :
        if calc <= K :
            break
        
        ans.append([1,i])
        calc -= 1
    
    for i in range(3,N) :
        if calc <= K :
            break
        
        for j in range(i+1,N+1) :
            if calc <= K :
                break
            
            ans.append([i,j])
            calc -= 1
    
    M = len(ans)
    
    print(M)
    
    for res in ans :
        print(*res)

Solution (Python):
def main():
    N, K = map(int, input().split())
    m = ((N-1)*(N-2)//2)
    if m < K:
        print(-1)
        return
    g = []
    for i in range(N-1):
        g.append((N, i+1))
    e = []
    for i in range(N-1):
        for j in range(i):
            e.append((i+1, j+1))
    print(N - 1 + m - K)
    for ans in (g + e[:(m-K)]):
        print('{} {}'.format(*ans))

main()

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

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

if ans == -1:
    print(ans)

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

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

if K > max:
    print('-1')
    sys.exit(0)

print(N-1 + max-K)


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

edges = []

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

for i in range(max-K):
    print("%d %d" % (edges[i][0], edges[i][1]))

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

Solution (Python):
import sys
from collections import Counter, deque, defaultdict
from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
from functools import reduce
from math import ceil, floor, log10, log2, factorial
from pprint import pprint

sys.setrecursionlimit(1000000)






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

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

if K > m:
    print(-1)
    sys.exit()

ans = []

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

c = m - K
cnt = 0

for i in range(1, N):
    for j in range(i + 1, N):
        if cnt == m - K:
            break
        ans.append((i, j))
        cnt += 1
    if cnt == m - K:
        break

print(len(ans))
for i, j in ans:
    print("{} {}".format(i, j))

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

f()

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

if (N - 1) * (N  - 2) // 2 < K:
    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 = M - (N - 1)
    start = 1
    while cnt != 0:
        for i in range(start, N - 1):
            print(start, i + 1)
            cnt -= 1
            if cnt == 0:
                break
        start += 1

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

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

Solution (Python):
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
g = []
lim_max = ((N - 1) * (N - 2)) // 2
if (K > lim_max):
  print(-1)
  exit()

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

count = lim_max - K

for i in range(2, N + 1):
  for j in range(3, N + 1):
    if (count == 0):
      break
    if (i != j and i < j):
      g.append((i, j))
      count -= 1


print(len(g))
for t in g:
  print(t[0], t[1])

Solution (Python):
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
g = []
high_limit = ((N - 1) * (N - 2)) // 2
if (K > high_limit):
  print(-1)
  exit()

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

count = high_limit - K

for i in range(2, N + 1):
  for j in range(3, N + 1):
    if (count == 0):
      break
    if (i != j and i < j):
      g.append((i, j))
      count -= 1


print(len(g))
for t in g:
  print(t[0], t[1])

Solution (Python):


import sys
import math

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

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

print((N - 1)*(N - 2) // 2 + (N - 1) - 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):
    print(s,t)
    t += 1
    if t > N:
        s += 1
        t = s + 1

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

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

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

  
  for i in range(N - 1):
    print('{} {}'.format(1, i + 2))

  u = 2
  v = u + 1
  
  for _ in range(MAX - K):
    print('{} {}'.format(u, v))
    v += 1
    if v > N:
      u += 1
      v = u + 1


if __name__ == '__main__':
  main()

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

Solution (Python):
def main():
  n, k = map(int, input().split())
  if n == 2:
    if k == 0:
      print(1)
      print('1 2')
    else:
      print(-1)
  elif k > ((n-1) * (n-2) // 2):
    print(-1)
  else:
    nedge = ((n-1) * (n-2)) // 2
    print(nedge - k + n-1)
    print('2 1')
    print('1 3')
    for j in range(4, n+1):
      print('1 %d' % j)
    is_match = False
    for j in range(2, n+1):
      for _k in range(j+1, n+1):
        if nedge == k:
          is_match = True
          break
        print(j, _k)
        nedge -= 1
      if is_match:
        break

if __name__ == '__main__':
  main()

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

Solution (Python):











import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N, K = MI()
    pair = ((N * (N - 1)) // 2)
    ans = []
    cnt = 0
    frag = False
    if ((N - 1) * (N - 2))//2 < K:
        print(-1)
        exit()
    else:
        for i in range(1, N):
            start = 0
            ans.append((start, i))
            cnt = (i * (i - 1)) // 2
    if cnt == K:
        print(len(ans))
        for (s, t) in ans:
            print(s+1, t+1)
    if cnt != K:
        for i in range(1, N):
            for j in range(i+1, N):
                ans.append((i, j))
                cnt -= 1
                if cnt == K:
                    print(len(ans))
                    for (s, t) in ans:
                        print(s+1, t+1)
            
if __name__ == '__main__':
    main()

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

main()

Solution (Python):
import itertools

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

if k > (n - 1) * (n - 2) // 2:
    print('-1')
else:
    v = list(range(2, n+1))

    pairs = list(itertools.combinations(v, 2))

    print(((n-1) * (n-2) // 2) - k + (n - 1))

    for i in range(2, n + 1):
        print('1 ' + str(i))
    
    for i in range(((n-1) * (n-2) // 2) - k):
        print(str(pairs[i][0]) + ' ' + str(pairs[i][1]))

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

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

li = []

for i in range(1, n+1):
    for j in range(i+1, n+1):
        li.append([i, j])

max_k = int(n * (n - 1) / 2)

M = max_k - k

print(M)

for i in range(M):
    print(li[i][0], li[i][1])

Solution (Python):


import sys

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

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

if k > maxPossible:
    print(-1)
    sys.exit()
ret = []


for i in range(2, n+1):
    ret.append((1, i))


count = 0
for i in range(2, n+1):
    if count == maxPossible - k:
        break
    for j in range(i+1, n+1):
        ret.append((i, j))
        count += 1
        if count == maxPossible - k:
            break

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

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

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

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

Solution (Python):

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

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

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

Solution (Python):

import sys
N,K=map(int,raw_input().split())

if K>sum(range(1,N-2+1)): 
    print(-1)
    sys.exit(0)
F=[]
for i in range(2,N+1):
    F.append((1,i))
n=sum(range(1,N-2+1))-K

c=0
for i in range(2,N+1):
    for j in range(i+1,N+1):
        if(c<n):
            F.append((i,j))
            c+=1
        else:
            break

print(len(F))
for u,v in F:
    print("%d %d"%(u,v))

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

Solution (Python):
import sys

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

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

Solution (Python):
import itertools
n, k = map(int, input().split())
 
def c(n):
    if n <= 2:
        return 1
    return n * (n - 1) // 2
 
 
if n > 2 and k <= c(n - 1):
    print(n - 1 + c(n - 1) - k)
    nodes = list(range(2, n + 1))
    for i in nodes:
        print(1, i)
 
    combi = itertools.combinations(nodes, 2)
    for i in range(c(n - 1) - k):
        print(*next(combi))
else:
    print("1\n1 2" if k == 0 else -1)

Solution (Python):
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True 
def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
if k>(n-1)*(n-2)//2:
    print(-1)
    exit()
print((n-1)*(n-2)//2 - k + n-1)
cnt = 0
print("{} {}".format(n-1,n))
for i in range(1,n-1):
    print("{} {}".format(i,n))
    for j in range(i+1,n):
        cnt += 1
        if cnt <= k:
            continue
        print("{} {}".format(i,j))

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

def c(n):
    if n <= 2:
        return 1
    return n * (n - 1) // 2


if n > 2 and k <= c(n - 1):
    print(n - 1 + c(n - 1) - k)
    nodes = list(range(2, n + 1))
    for i in nodes:
        print(1, i)

    combi = itertools.combinations(nodes, 2)
    for i in range(c(n - 1) - k):
        print(*next(combi))
else:
    print("1\n1 2" if k == 0 else -1)

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa == x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))

import fractions
from functools import reduce
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

n,k = m() 

l = []
a = (n-1)*(n-2)//2
if k > a:
    print(-1)
else:
    print(n-1 + a - k)
    co = a - k
    for i in range(2,n+1):
        l.append([1,i])
    for i in range(2,n+1):
        if co == 0:
            break
        for j in range(i+1,n+1):
            l.append([i,j])
            co -= 1
            if co == 0:
                break
        if co == 0:
            break
    for i in range(len(l)):
        print(jo(l[i]))

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

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

Solution (Python):
def e_friendships(N, K):
    if K > (N - 1) * (N - 2) // 2:
        return -1

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

    def append_edge(t, lst=[], count=0):  
        for i in range(2, N):
            for j in range(i + 1, N + 1):
                if count == t:
                    return lst
                lst.append((i, j))
                count += 1
        return lst

    ans_graph += append_edge((N - 1) * (N - 2) // 2 - K)
    ans_graph.insert(0, (len(ans_graph),))
    return '\n'.join([' '.join(map(str, row)) for row in ans_graph])

N, K = [int(i) for i in input().split()]
print(e_friendships(N, K))

Solution (Python):
N,K = map(int,input().split())
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
if N == 2:
    if K == 0:
        print(1)
        print('1 2')
    else:
        print(-1)
else:
    uni = combinations_count(N-1,2)
    if uni < K:
        print(-1)
    else:
        ans = []
        for i in range(2,N+1):
            ans.append([1,i])
        kai = uni-K
        for i in range(2,N+1):
            for j in range(2,N+1):
                if kai > 0:
                    if i < j:
                        ans.append([i,j])
                        kai -= 1
                else:
                    break
        print(len(ans))
        for r in ans:
            print(' '.join(map(str,r)))

Solution (Python):
def e_friendships(N, K):
    if K > (N - 1) * (N - 2) // 2:
        return -1

    ans_graph = []
    for i in range(2, N + 1):
        ans_graph.append('{} {}'.format(1, i))  

    def append_edge(t, lst=[]):
        count = 0
        for i in range(2, N):
            for j in range(i + 1, N + 1):
                if count == t:
                    return lst
                lst.append('{} {}'.format(i, j))
                count += 1
        return lst

    ans_graph = append_edge((N - 1) * (N - 2) // 2 - K, ans_graph)
    return str(len(ans_graph)) + '\n' + '\n'.join(ans_graph)

N, K = [int(i) for i in input().split()]
print(e_friendships(N, K))

Solution (Python):
n,k = map(int,input().split())
def kai(x):
    ans = 1
    for i in range(x):
        ans *= i+1
    return ans
def kc2(k):
    return int(kai(k) /(2*kai(k-2)))

n_1c2 = kc2(n-1)

if k > n_1c2:
    print(-1)
else:
    lis = []
    for i in range(1,n):
        lis.append([1,i+1])
    cnt = 2
    now = 2
    for j in range(1,n_1c2 - k +1):
        cnt += 1
        if cnt > n:
            now +=1
            cnt = now +1
        lis.append([now,cnt])
    print(len(lis))
    for u,v in lis:
        print(u,v)

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

ans = []
M = N - 1

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

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

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

for i in range(2, N):
    for j in range(i + 1, N + 1):
        if M - N + 1 >= a:
            break
        ans.append([i, j])
        M += 1

print(M)
for aa in ans:
    print(aa[0], aa[1])

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(N):
    for j in range(i+1, N):
        edges.append((i+1, j+1))

if K>(N-1)*(N-2)//2:
    print(-1)
    exit()
else:
    for i in range(K):
        edges.pop()

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

Solution (Python):
from itertools import combinations
inf = input().split()
N = int(inf[0])
K = int(inf[1])
if K > (N-1)*(N-2)//2:
  print(-1)
else:
  if K==(N-1)*(N-2)//2:
    print(N-1)
    for i in range(2,N+1):
      print(1,i)
  else:
    ls = [i for i in range(1,N+1)]
    ls = list(combinations(ls,2))
    dic = {x:0 for x in ls}
    for i in range(2,N+1):
      dic[(1,i)] = 1
    m = (N-1)*(N-2)//2
    while m>K:
      for x in ls:
        if dic[x]==0:
          dic[x]=1
          m-=1
          break
    ans = [i for i in dic.keys() if dic[i]==1]
    print(len(ans))
    for x in ans:
      print(x[0], x[1])