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

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

    cnt = n - 1
    for u in range(2, n + 1):
        for v in range(u + 1, n + 1):
            if k:
                g[u][v] = 0
                k -= 1
            cnt += g[u][v]

    print(cnt)
    for u in range(1, n + 1):
        for v in range(u + 1, n + 1):
            if g[u][v]:
                print(u, v)

else:
    print(-1)

Solution (Python):
from itertools import combinations

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

if k > (n - 1) * (n - 2) // 2:
  print(-1)
else:
  print(n * (n - 1) // 2 - k)
  for i in range(1, n):
    print(i, n)
  edge = combinations(range(1, n), 2)
  for _ in range((n - 1) * (n - 2) // 2 - k):
    u, v = next(edge)
    print(u, v)

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

Solution (Python):
from sys import stdin, setrecursionlimit
from collections import deque


def main():
    INF = float('inf')
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    if k > (n - 1) * (n - 2) // 2:
        print(-1)
    else:
        ab = [[i, j] for i in range(1, n) for j in range(i + 1, n + 1)]
        m = n - 1 + (n - 1) * (n - 2) // 2 - k
        print(m)
        for i, j in ab[:m]:
            print(i, j)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Solution (Python):

import sys


buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


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


def read_float():
    return float(buff_readline())


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


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


def read_str_n():
    return readline().strip().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):
    from itertools import combinations
    ans = [(1, i) for i in range(2, N+1)]

    K -= (N-1)*(N-2) // 2
    if K > 0:
        print(-1)
        return
    for i, j in combinations(range(2, N+1), r=2):
        if K == 0:
            break
        ans.append((i, j))
        K += 1


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



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


if __name__ == '__main__':
    main()

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

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

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

if k > max_val:
    print(-1)
    exit()
else:
    cnt = (n - 1) * (n - 2) // 2 - k
    m = n - 1 + cnt
    print(m)
    for i in range(n-1):
        print(1, i+2)
    if cnt == 0:
        exit()
    tmp = 0

    for j in range(2, n + 1):
        for p in range(j + 1, n + 1):
            print(j, p)
            tmp += 1
            if tmp == cnt:
                exit()

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


MAX_PAIR = (n-1)*(n-2)//2
if MAX_PAIR < k:
    print(-1)
else:
    m = n-1+MAX_PAIR-k
    print(m)
    for v2 in range(2, n+1):
        print(1, v2)

    cnt = MAX_PAIR-k
    for v, v2 in combinations(range(2, n+1), 2):
        if cnt == 0:
            break
        cnt -= 1
        print(v, v2)

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


MAX_PAIR = (n-1)*(n-2)//2
if MAX_PAIR < k:
    print(-1)
else:
    m = n-1+MAX_PAIR-k
    print(m)
    for v2 in range(2, n+1):
        print(1, v2)
        m -= 1
    if m == 0:
        exit()

    for v in range(2, n):
        for v2 in range(v+1, n+1):
            print(v, v2)
            m -= 1
            if m == 0:
                exit()

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

if n == 2:
    if k == 0:
        print(1)
        print(1, 2)
    else:
        print(-1)
    exit()

MAX_PAIR = (n-1)*(n-2)//2
if MAX_PAIR < k:
    print(-1)
else:
    m = n-1+MAX_PAIR-k
    print(m)
    for v2 in range(2, n+1):
        print(1, v2)
        m -= 1
    if m == 0:
        exit()

    for v in range(2, n):
        for v2 in range(v+1, n+1):
            print(v, v2)
            m -= 1
            if m == 0:
                exit()

Solution (Python):
N,K=map(int,input().split())
if int((N-1)*(N-2)/2)<K:
 print(-1)
else:
 if N==2:
  print(1)
  print(*[1,2])
 else:
  S=int((N-1)*(N-2)/2)-K
  print(N-1+S)
  for i in range(N-1):
   print(*[1,2+i])
  t=0
  l=2
  r=3
  while t<S:
   print(*[l,r])
   t+=1
   if r==N:
    l+=1
    r=l+1
   else:
    r+=1

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

graph = [[] for i in range(n)]
if k > ((n-1)*(n-2))//2:
    print(-1)
else:
    for i in range(1,n):
        graph[0].append(i+1)
    
    remain = ((n-1)*(n-2))//2 - k
    for i in range(1,n):
        if remain <= 0:
            break
        for j in range(i+1,n):
            if remain > 0:
                graph[i].append(j+1)
                remain -= 1
            else:
                break

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

Solution (Python):









from itertools import combinations
N, K = map(int, input().split())
k_max = ((N - 1) * (N - 2)) // 2

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

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


for u, v in combinations(range(1, N), r=2):
    if k == K:
        break
    k -= 1
    ans.append((u + 1, v + 1))

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

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, k = map(int, readline().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 + 1):
    for j in range(i + 1, n + 1):
        print(i, j)
        m -= 1
        if m == 0:
            exit()

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    n,k=map(int,readline().split())

    if k>(n-1)*(n-2)//2:
        print(-1)
    else:
        m=n-1
        G=[]
        for i in range(2,n+1):
            G.append((1,i))
        cnt=(n-1)*(n-2)//2

        i=2
        j=3
        while k!=cnt:
            G.append((i,j))
            if j<n:
                j+=1
            else:
                i+=1
                j=i+1
            m+=1
            cnt-=1

        print(m)
        for i in range(m):
            print(*G[i])
            
if __name__=="__main__":
    main()

Solution (Python):
N, K = map(int, input().split(' '))
count = (N - 1) * (N - 2) // 2
is_connect = [[False for _ in range(N)] for _ in range(N)]
if K > count:
  print(-1)
else:
  for i in range(1, N):
    is_connect[0][i] = True
  i = 2
  j = 1
  for _ in range(count - K):
    is_connect[j][i] = True
    if i == N - 1:
      j += 1
      i = j + 1
    else:
      i += 1
  print(N - 1 + count - K)
  for i in range(N):
    for j in range(N):
      if i != j and is_connect[i][j]:
        print('{0} {1}'.format(i + 1, j + 1))

Solution (Python):
import sys
N, K = map(int, input().split())
MAXP = (N - 1) * (N - 2) // 2
res = MAXP - K

if res < 0:
    print(-1)
    sys.exit()

M = (N - 1) + res

root = 2
end = root + 1

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

for _ in range(res):
    if end > N:
        root += 1
        end = root + 1
    print(root, end)
    end += 1

Solution (Python):

import sys
import numpy as np
from itertools import combinations


def solve(N: int, K: int):
    if K > (N-1) * (N-2) // 2:
        print(-1)
        return
    print(N-1 + ((N-1) * (N-2) // 2 - K))
    for n in range(2, N+1):
        print(1, n)
    count = 0
    for c in combinations(range(2, N+1), 2):
        if (N-1) * (N-2) // 2 - K == count:
            return
        count += 1
        print(c[0], c[1])
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    solve(N, K)

if __name__ == '__main__':
    main()

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


def read():
    N, K = map(int, input().strip().split())
    return N, K


def binom2(n):
    return n * (n-1) // 2


def print_graph(pairs):
    print(len(pairs))
    for x, y in pairs:
        print("%d %d" % (x, y))


def solve(N, K):
    B = binom2(N-1)
    if K > B:
        print(-1)
        return
    XY = []
    y = N
    for x in range(1, N):
        XY.append((x, y))
    k = B
    
    for x in range(1, N):
        for y in range(x+1, N):
            if k == K:
                print_graph(XY)
                return
            XY.append((x, y))
            k -= 1
    print_graph(XY)


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

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

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

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

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

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

    
    for i in range(max_K - K):
        a, b = cand.pop()
        ans.append((a,b))

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



if __name__ == "__main__":
    main()

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N,K = LI()
    sa = (N - 1) * (N - 2) // 2 - K
    if sa < 0:
        print(-1)
        return

    ans = []
    for i in range(2,N+1):
        ans.append((1,i))
    i = 2
    j = 3
    while sa > 0:
        ans.append((i,j))
        sa -= 1
        j += 1
        if j > N:
            i += 1
            j = i + 1
    print(len(ans))
    for x in ans:
        print(x[0],x[1])

if __name__ == '__main__':
    main()

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    if (N - 1) * (N - 2) // 2 < K:
        print(-1)
        return
    else:
        edges = set()
        for i in range(2, N + 1):
            edges.add((1, i))

        cnt = (N - 1) * (N - 2) // 2
        for i in range(1, N):
            for j in range(i, N + 1):
                if i == j:
                    continue

                if cnt == K:
                    print(len(edges))
                    for a, b in edges:
                        print(a, b)
                    return

                if (i, j) not in edges:
                    cnt -= 1
                    edges.add((i, j))

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


if __name__ == "__main__":
    main()

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

def ans(n,k):  
  ans = (n-1) + ((n-1)*(n-2)//2 - k)
  print(ans)
  c = 0
  for i in range(n):
    for j in range(i+1,n):
      if c >= ans:
        return 
      print(i+1,j+1)
      c += 1

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

Solution (Python):



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

"""
ノード１にノード２からNまでをつなぐ時が、距離２の組を最大にできるつなぎ方（のはず）で、nCk(N-1, 2)組できる。
あとはKがひとつ減るごとに、２～Nの間に辺を追加位していけばよいか

"""
if (N-1)*(N-2)//2 < K:
    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
for i in range(2, 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):
n, k = [int(i) for i in input().split()]
if k > (n - 1)*(n - 2) // 2:
    print(-1)
else:
    ed = []
    for i in range(1, n):
        ed.append(" ".join(map(str, [1, i + 1])))
    kk = (n - 1)*(n - 2)//2 - k
    if not kk:
        print(len(ed))
        print("\n".join(ed))
        exit()
    for i in range(1, n):
        for j in range(i+1, n):
            ed.append(" ".join(map(str, [i + 1, j + 1])))
            kk -= 1
            if not kk:
                print(len(ed))
                print("\n".join(ed))
                exit()

Solution (Python):
from itertools import combinations 
n,k=map(int,input().split())
m = (n-1)*(n-2)//2
if k>m:
    print(-1)
else:
    es = []
    for i in range(2,n+1):
        es.append('1 %d' % i)
        
    idx = 0
    for i,j in combinations(range(2,n+1),2):
        if idx == m-k:
            break
        es.append('%d %d' % (i,j))
        idx +=1
    print(len(es))
    print(*es,sep="\n")

Solution (Python):
from itertools import combinations 
n,k=map(int,input().split())
m = (n-1)*(n-2)//2
if k>m:
    print(-1)
else:
    es = []
    for i in range(2,n+1):
        es.append('1 %d' % i)
        
    idx = 0
    for i,j in combinations(range(2,n+1),2):
        if idx == m-k:
            break
        es.append('%d %d' % (i,j))
        idx +=1
    print(len(es))
    print(*es,sep="\n")

Solution (Python):
from itertools import combinations 
n,k=map(int,input().split())
m = (n-1)*(n-2)//2
if k>m:
    print(-1)
else:
    es = []
    for i in range(2,n+1):
        es.append((1,i))
        
    idx = 0
    for i,j in combinations(range(2,n+1),2):
        if idx == m-k:
            break
        es.append((i,j))
        idx +=1
    print(len(es))
    for i,j in es:
        print(i,j)

Solution (Python):
import itertools
N,K=map(int,input().split())
c=(N-1)*(N-2)//2
if c<K:
    print(-1)
    exit()
Edges=[]
for i in range(1,N):
    Edges.append([1,i+1])
cnt=0
for x in itertools.combinations(range(1,N),2):
    if cnt==c-K:
        break
    Edges.append([x[0]+1,x[1]+1])
    cnt+=1
print(len(Edges))
for x,y in Edges:
    print(x,y)

Solution (Python):

import sys

sys.setrecursionlimit(10 ** 8)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, K = map(int, readline().split())
KMAX = (N - 1) * (N - 2) // 2


def solve():
    if K > KMAX:
        return None
    edges = []
    for i in range(1, N):
        edges.append((1, i + 1))
    if K == KMAX:
        return edges
    cnt = KMAX
    for i in range(1, N):
        for j in range(i + 1, N):
            edges.append((i + 1, j + 1))
            cnt -= 1
            if cnt == K:
                return edges
    raise ValueError()


if __name__ == "__main__":
    r = solve()
    if r is None:
        print(-1)
    else:
        print(len(r))
        for u, v in r:
            print(u, v)

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

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


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


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

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

count=0
flag=False
for i in range(2,N):
    for j in range(i+1,N+1):
        if count<res:
            G.append((i,j))
            count+=1
        if count==res:
            flag=True
            break
    if flag:
        break

print(len(G))
for t in G:
    a,b=t
    print(a,b)

Solution (Python):
n,k = map(int,input().split())
if n == 2 and k == 0:
  print(1)
  print(1,2)
  exit()
  
if k > (n-1)*(n-2)//2 or n == 2:
  print(-1)
  exit()
  
if n == 3:
  if k == 1:
    print(2)
    print(1,2)
    print(2,3)
  elif k == 0:
    print(3)
    print(1,2)
    print(2,3)
    print(3,1)
  else:
    print(-1)
  exit()
    
m = (n-1)*(n-2)//2+(n-1)-k
print(m)
for i in range(n-1):
  print(1,2+i)
for i in range(min(n-1,m-n+1)):
  if 3+i > n:
    print(2+i,2)
  else:
    print(2+i,3+i)
    
if m > (n-1)*2:
  cnt = m-(n-1)*2
  for i in range(2,n-2):
    if cnt == 0:
        break
    for j in range(2,n-i+1):
      print(j,j+i)
      cnt -= 1
      if cnt == 0:
        break

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

def solve():
    if K>(N-1)*(N-2)//2:
        print(-1)
        return
    M = (N-1)*N//2-K
    print(M)
    for i in range(1,N):
        print(i,N)
    edge = N-1
    if edge==M:
        return
    for i in range(1,N-1):
        for j in range(i+1,N):
            print(i,j)
            edge += 1
            if edge==M:
                return
solve()

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

    edges = []
    for u in range(1, N):
        edges.append((0, u))

    count = (N - 1) * (N - 2) // 2
    to_join = count - K

    v = 1
    while v < N and to_join:
        for u in range(v + 1, N):
            edges.append((v, u))
            to_join -= 1
            if to_join == 0: break
        v += 1

    if to_join:
        print(-1)
    else:
        print(len(edges))
        for edge in edges:
            print(*map(lambda x: x + 1, edge))


if __name__ == '__main__':
    main()

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

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

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

cnt = (n - 1) * (n - 2) // 2 - k
for i in range(2, n):
    for j in range(i + 1, n + 1):
        if cnt == 0:break
        ans.append((i, j))
        cnt -= 1
    if cnt == 0:break

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

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

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

nokori = int(maxi - k)
print(nokori + n-1)
for i in range(1, n):
    print(i, n)

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

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
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
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

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

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


if d < K:
    print(-1)
    exit(0)

ret = N-1 + (d - K)
print(ret)

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

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

if __name__ == "__main__":
    main()

Solution (Python):
def main():
    N, K = (int(i) for i in input().split())
    if (N-1)*(N-2)//2 < K:
        return print(-1)
    need_reduce = (N-1)*(N-2)//2 - K
    M = (N-1) + need_reduce
    ans = [[1, j] for j in range(2, N+1)]
    i = 2
    while need_reduce > 0:
        j = i+1
        while j <= N and need_reduce > 0:
            ans.append([i, j])
            j += 1
            need_reduce -= 1
        i += 1
    print(M)
    for a in ans:
        print(*a)


if __name__ == '__main__':
    main()

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

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

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

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

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

flag = True
for i in range(2, N):
    for j in range(i + 1, N + 1):
        if K < NOW:
            G.append([i, j])
            NOW -= 1
        else:
            flag = False
            break
    if not flag:
        break

print (len(G))
for tmp in G:
    print (*tmp, sep = ' ')

Solution (Python):
def main():
    N, K = (int(i) for i in input().split())
    if (N-2)*(N-1)//2 < K:
        return print(-1)
    need_reduce = (N-2)*(N-1)//2 - K
    M = (N-1) + need_reduce
    ans = [(1, j) for j in range(2, N+1)]
    i = 2
    while need_reduce > 0:
        j = i+1
        while j <= N and need_reduce > 0:
            ans.append((i, j))
            j += 1
            need_reduce -= 1
        i += 1
    print(M)
    for a in ans:
        print(*a)


if __name__ == '__main__':
    main()

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

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

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

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

m = n-1 + (n-1)*(n-2)//2 - k
print(m)
for i in range(m):
    print(*E[i])

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

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

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

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

m = n-1 + (n-1)*(n-2)//2 - k
print(m)
for i in range(m):
    print(*E[i])

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
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
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7


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

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

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

is_ban = [[False]*N for _ in range(N)]

for i in range(1, N):
    for j in range(i+1, N):
        if K==0:
            break
        
        is_ban[i][j] = True
        K -= 1

ans = []

for i in range(N):
    for j in range(i+1, N):
        if not is_ban[i][j]:
            ans.append((i, j))
    
print(len(ans))

for u, v in ans:
    print(u+1, v+1)

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

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

edges = [(1, to) for to in range(2, N + 1)]
M = (N - 1) * (N - 2) // 2

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

for fr in range(2, N + 1):
    for to in range(fr + 1, N + 1):
        if M == K:
            break
        edges.append((fr, to))
        M -= 1

print(len(edges))
for fr ,to in edges:
    print(fr ,to)

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

Solution (Python):
n,k = map(int,input().split())
mx  = (n-1)*(n-2)//2
li  = []

if k>mx:
  print('-1')
else:
  e = mx-k
  M = (n-1)+e
  print(M)

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

  for i in range(1,n):
    print(n,i)
  
  for i in range(e):
    print(*li[i])

Solution (Python):

import sys
import itertools
N,K=map(int, sys.stdin.readline().split())
cnt=(N-1)*(N-2)/2 
if cnt<K:
    print -1
    quit()
L=list( itertools.combinations(range(1,N+1),2) )

M=N-1+cnt-K
print M
for i in range(N-1):
    print L[i][0],L[i][1]

for i in range(N-1,N-1+cnt-K):
    print L[i][0],L[i][1]

Solution (Python):





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

maxval = ((N-1)*(N-2)) // 2
if maxval < K:
  print(-1)
  exit(0)
  
ans = []


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

goal = maxval - K
cnt = 0
import itertools
for comb in itertools.combinations(range(2,N+1),2):
  if goal == cnt:
    break
  ans.append(comb)
  cnt += 1
  
print(len(ans))
for a in ans:
  print(*a)

Solution (Python):





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

maxval = ((N-1)*(N-2)) // 2
if maxval < K:
  print(-1)
  exit(0)
  
ans = []


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

goal = maxval - K
i = 2
j = 3
while goal > 0:
  ans.append((i,j))
  if j + 1 > N:
    i += 1
    j = i + 1
  else:
    j += 1
  goal -= 1
  
print(len(ans))
for a in ans:
  print(*a)

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

limit=(N*(N-1)//2)-(N-1)
Ans=[]

for index in range(2,N+1):
    Ans.append((1,index))

if K>limit:
    print('-1')
else:
    rest=limit-K
    From=2
    To=3

    while rest:
        Ans.append((From,To))
        To+=1
        if To==N+1:
            From+=1
            To=From+1
        rest-=1
    
    print(len(Ans))
    for i in range(len(Ans)):
        print('{0} {1}'.format(Ans[i][0],Ans[i][1]))

Solution (Python):
from itertools import combinations

n, k = map(int, input().split())
star = []
max_k = (n - 1) * (n - 2) // 2
delta = max_k - k
cnt = 0
if k <= max_k:
    ans = []
    for i in range(2, n + 1):
        star.append((1, i))
        cnt += 1

    if delta > 0:
        kumiawase = list(combinations(range(2, n+1), 2))
        for j in range(delta):
            kumi = kumiawase[j]
            star.append(kumi)
            cnt += 1
    print(cnt)
    for pair in star:
        print(*pair, sep=" ")
else:
    print(-1)

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

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

ef, et = 1, 2
for i in range(cnt - K):
    edges[ef].append(et)
    et += 1
    if et == ef:
        et += 1
    if et == N:
        ef += 1
        et = ef + 1

M = sum([len(edge) for edge in edges])
print(M)
for u in range(N):
    for v in edges[u]:
        print(u + 1, v + 1)