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 itertools
N, K = [int(_) for _ in input().split()]
if (N - 1) * (N - 2) // 2 < K:
    print(-1)
    exit()
ans1 = []
ans2 = []
for i in range(1, N):
    ans1 += [[i, N]]
for ab in itertools.combinations(range(1, N), 2):
    a, b = ab
    ans2 += [[a, b]]
ans = ans1 + ans2[:len(ans2) - K]
print(len(ans))
for ab in ans:
    a, b = ab
    print(a, b)

Solution (Python):
import math
import sys
input=sys.stdin.readline
from collections import deque
from heapq import heappush,heappop
import re

def int_raw():
    return int(input())
 
def ss_raw():
    return input().split()
 
def ints_raw():
    return tuple(map(int, ss_raw()))


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


DIV = 10**9+7

def ncr(n, r, mod=DIV):
	r = min(r, n-r)
	ret = 1
	for i in range(1, r+1):
		ret = ret * (n-i+1) % mod
		ret = ret * mod_inv(i, mod) % mod
	return ret
    
def mod_modify(a,mod):
    return (a+mod)%mod

def cal_ABC(A,B,C):
    AC = A//C + (1 if A%C>0 else 0)
    BC = (B//C)
    return max(BC-AC+1,0)


def gcd(a, b):
    if(b == 0):
        return a
    return gcd(b, a % b)

def main():
    N,K = ints_raw()
    MAX2 = (N-1)*(N-2)//2
    if K > MAX2:
        print(-1)
        return
    print(N-1+MAX2-K)
    for idx in range(2,N+1):
        print (1,idx)
    b = 0
    for idx in range(2,N+1):
        for jdx in range(idx+1,N+1):
            if b >= MAX2-K:
                return 
            print (idx,jdx)
            b+=1
            
main()

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

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

def answer(n, k, m):
    o = 1
    while m != 0:
        for i in range(n-o):
            print('{} {}'.format(o, i + o + 1))
            m -= 1
            if m == 0:
                break
        o += 1
    
E = comb(N)
if E < K:
    print(-1)
else:
    M = N - 1 + (E - K) 
    print(M)
    answer(N, K, M)

Solution (Python):
import sys
n,k = map(int,input().split())
c = (n-1)*(n-2) // 2
if c < k:print(-1);sys.exit()
count = 2
print(n-1 + c - k)
for i in range(n-1):
    print(1,count)
    count += 1
count = 0
if c == k:sys.exit()
for i in range(2,n+1):
    oo = False
    for j in range(i + 1,n + 1):
        count += 1
        print(i,j)
        if count >= c - k:oo = True;break
    if oo:break

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

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

    v = 1
    while tar >0:
        for i in range(v+1,n):
            print(v,i)
            tar = tar-1
            if tar == 0:
                break
        v += 1

Solution (Python):
def cmb(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

N, K = list(map(int, input().split()))
k_max = int(cmb(N - 1, 2))

if N == 2:
    if K == 0:
        print(1)
        print(1, 2)
    else:
        print(-1)
elif k_max < K:
    print(-1)
else:
    E = [[v, N] for v in range(1, N)]

    add_need = k_max - K
    if add_need >= 1:
        count = 0
        for i in range(1, N):
            for k in range(i + 1, N):
                E.append([i, k])
                count += 1
        
                if count == add_need:
                    break
            if count == add_need:
                break

    print(len(E))
    for e in E:
        print(e[0], e[1])

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

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

p = int((N-1)*(N-2)/2-K)
M = N-1+p
print(M)

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

z = 1
zz = 2
for i in range(p):
    print(z, zz)
    zz += 1
    if zz > N-1:
        z += 1
        zz = z+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
    Chk[i-1][0]=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
          Chk[j-1][i-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):
n, k = map(int, input().split())
ans = []
c = (n - 1) * (n - 2) // 2 - k
if c < 0:
    print(-1)
    exit(0)
for i in range(1, n):
    ans.append((0, i))
cnt = 0
flg = 0
for i in range(1, n):
    if flg:
        break
    for j in range(i + 1, n):
        if cnt >= c:
            flg = 1
            break
        ans.append((i, j))
        cnt += 1
print(len(ans))
for a, b in ans:
    print(a + 1, b + 1)

Solution (Python):


import itertools
get = lambda: int(raw_input().strip())
getl = lambda: raw_input().strip()
gets = lambda: map(int, raw_input().strip().split())
getss = lambda n: [gets() for _ in xrange(n)]


N, K = gets()

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

res.extend(list(itertools.combinations(range(1, N), 2))[:total - K])
print len(res)
for i, j in res:
    print i, j

Solution (Python):
import bisect
from operator import itemgetter
import math
import collections
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def combi(a,b):
  C = 1
  for j in range(b):
    C = C*(a-j)//(j+1)
  return C

N,K = IL()

num = combi(N-1,2)
if num < K:
  print(-1)
  exit()
else:
  print(N-1 + num-K)
  for i in range(2,N+1):
    print(1,i)
data = [i for i in range(2,N+1)]

for i in itertools.combinations(data,2):
  if K == num:
    break
  print(i[0],i[1])
  num -= 1

Solution (Python):
n,k=list(map(int,input().split()))
if k>(n-1)*(n-2)/2:
    print("-1")
else:
    s=int((n-1)*(n-2)/2-k)
    a=[]
    for i in range(n):
        for j in range(i+1,n):
            a.append([i+1,j+1])
    print(n-1+s)
    for i in range(n-1+s):
        print("%d %d" %(a[i][0],a[i][1]))

Solution (Python):
n, k = [ int(v) for v in input().split() ]
kmax, kmin = (n-1)*(n-2)//2, 0

if k > kmax:
    print(-1)
else:
    anslist = [ (i+1,j+1) for i in range(n) for j in range(n) if i < j ]
    m = len(anslist)
    print(m-k)
    for i in range(m-k):
        print(*anslist[i])

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



def main():
    if K == 0:
        M = N * (N-1) // 2
        print(M)
        for i in range(1, N+1):
            for j in range(i+1, N+1):
                print("%d %d" % (i, j))
        return
    if N == 2 or K > (N-1) * (N-2) // 2:
        M = -1
        print(M)
        return

    M = N * (N-1) // 2 - K
    print(M)
    count = 0
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            if 1 not in [i, j] and count < K:
                count += 1
            else:
                print(i, j)

main()

Solution (Python):

import sys
import math
import numpy as np
import itertools


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

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

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

print(m)

u=0
v=1
for i in range(m):
    print(u+1,v+1)
    v += 1
    if v==n:
        u += 1
        v = u + 1

Solution (Python):
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

N, K = nl()
import sys

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

add = (N - 2) * (N - 1) // 2 - K
M = N - 1 + add
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 add == 0:
            break
        print(i, j)
        add -= 1
    if add == 0:
        break

Solution (Python):
import itertools

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

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

it = itertools.combinations(range(1,N), 2)

cnt = 0
for pair in it:
    if cnt >= al - K:
        break
    cnt += 1
    
    result.append(pair)


print(len(result))
for r in result:
    print(r[0], r[1])

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

N,K=map(int,input().split())
ANS=[]

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

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

NOW=2
while True:
    
    if K<=N-NOW:
        for i in range(NOW+1,N-K+1):
            ANS.append((NOW,i))

        for i in range(NOW+1,N):
            for j in range(i+1,N+1):
                ANS.append((i,j))

        break

    else:
        K-=(N-NOW)
        NOW+=1

ANS=list(set(ANS))
print(len(ANS))
for ans in ANS:
    print(*ans)

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

Solution (Python):

def main():
    buf = input()
    buflist = buf.split()
    N = int(buflist[0])
    K = int(buflist[1])
    edge = dict()
    for i in range(N):
        edge[i] = set()
        for j in range(i+1, N):
            edge[i].add(j)
    removed = 0
    for i in range(N):
        for j in range(i+1, N-1):
            if removed < K:
                edge[i].remove(j)
                removed += 1
    if removed < K:
        print(-1) 
        return
    print((N * (N - 1)) // 2 - K)
    for i in range(N):
        for x in edge[i]:
            print(i+1, x+1)

if __name__ == '__main__':
    main()

Solution (Python):
import sys


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

maxcnt = (N - 1) * (N - 2) // 2
if K > maxcnt:
    print(-1)
else:
    print((N - 1) + maxcnt - K)
    for i in range(1, N):
        print(1, i + 1)
    
    q = []
    for i in range(1, N - 1):
        for j in range(i + 1, N):
            q.append((i + 1, j + 1))
    while maxcnt > K:
        i, j = q.pop()
        print(i, j)
        maxcnt -= 1

Solution (Python):
def li():
    return list(map(int, input().split()))


if __name__ == "__main__":
    [n, k] = li()
    if k > (n - 1) * (n - 2) // 2:
        print(-1)
    else:
        mesh_edge_list = []
        for i in range(n):
            for j in range(i + 1, n):
                mesh_edge_list.append((i, j))
        for _ in range(k):
            del_index = -1
            for index, edge in enumerate(mesh_edge_list):
                if edge[1] != n - 1:
                    del_index = index
                    break
            mesh_edge_list.pop(del_index)

        print(n * (n - 1) // 2 - k)
        for edge in mesh_edge_list:
            print(edge[0] + 1, edge[1] + 1)

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

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

else:
    L = ((N-1)*(N-2))//2 - K
    S = 0
    SL = []
    for i in range(1,N):
        S+=i
        SL.append(S)
    now = N-1
    now2 = 1
    ans = []
    j = 0
    for i in range(L):
        ans.append([now,now+now2])
        if SL[j]==i+1:
            j+=1
            now-=1
            now2=1
        else:
            now2+=1
    
    print(len(ans)+N-1)
    for i in range(2,N+1):
        print(1,i)
    for a in ans:
        print(*a)

Solution (Python):
def solve():
  N, K = map(int, input().split())
  maxK = sum(range(1, N - 1))
  if maxK < K:
    print(-1)
    return
  M = N - 1 + (maxK - K)
  print(M)
  for i in range(1, N):
    for j in range(i + 1, N + 1):
      if M == 0: return
      print(i, j)
      M -= 1

solve()

Solution (Python):
N, K = map(int, input().split())
Max = N*(N-1)//2-(N-1)
if Max<K:
    print(-1)
else:
    d = Max-K
    print(N-1+d)
    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 d>0:
                print(i, j)
                d-=1
            else:
                break

Solution (Python):
def solve(n, k):
    mx = (n - 1) * (n - 2) // 2
    if k > mx:
        print(-1)
        return
    buf = []
    for i in range(2, n + 1):
        buf.append('1 {}'.format(i))

    p = 2
    q = 3
    for _ in range(mx - k):
        buf.append('{} {}'.format(p, q))
        if q >= n:
            p += 1
            q = p + 1
        else:
            q += 1

    print(len(buf))
    print('\n'.join(buf))


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

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

def f():
    L = (N - 1) * (N - 2) // 2
    if K > L:
        print(-1)
        return

    res = ""
    for i in range(1, N):
        res += "{0} {1}\n".format(1, i + 1)

    R = L - K
    M = N - 1 + R

    r = 0
    for i in range(1, N - 1):
        for j in range(i + 1, N):
            if r == R:
                print(M)
                print(res)
                return
            res += "{0} {1}\n".format(i + 1, j + 1)
            r += 1
    print(M)
    print(res)

f()

Solution (Python):

import sys
from itertools import combinations


def solve(N: int, K: int):
    if K > (N-2) * (N - 1) // 2:
        print(-1)
        return
    print(N * (N-1) // 2 - K)
    count = 0
    for u, v in combinations(range(1, N+1), 2):
        if u == 1:
            print(u, v)
            continue
        if count < K:
            count += 1
            continue
        print(u, v)
    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):
def getlist():
	return list(map(int, input().split()))

N, K = getlist()

if N == 2:
	if K == 0:
		print(1)
		print(1, 2)
	else:
		print(-1)
else:
	if K > int((N - 1) * (N - 2) // 2):
		print(-1)
	else:
		M = N - 1 + (int((N - 1) * (N - 2) // 2) - K)
		print(M)
		for i in range(1, N):
			print(i, N)
		x = int((N - 1) * (N - 2) // 2) - K
		v = 1
		for i in range(1, N):
			for j in range(i + 1, N):
				if v <= x:
					print(i, j)
					v += 1
				else:
					break

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


def solve(N, K):
    edge = [(1, i+2) for i in range(N-1)]
    max_num = (N - 1) * (N - 2) // 2
    now = 2
    check = 1
    for i in range(max_num - K):
        if now + check > N:
            now += 1
            check = 1
        edge.append((now, now + check))
        check += 1
    return edge


if (N - 1) * (N - 2) // 2 < K:
    print(-1)
else:
    ans = solve(N, K)
    print(len(ans))
    for i in range(len(ans)):
        print('{} {}'.format(ans[i][0], ans[i][1]))

Solution (Python):
n, k = map(int, input().split())
uni = [[] for i in range(n)]
ans = []

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

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

max_k = 0
for i in range(n-2):
    max_k += i + 1
if k > max_k:
    print(-1)
else:
    cnt = max_k - k
    while True:
        for i in range(1,n):
            for j in range(i+1, n):
                if cnt == 0:
                    print(len(ans))
                    for num in ans:
                        print(" ".join(map(str, num)))
                    exit()
                ans.append([i+1, j+1])
                cnt -= 1

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

if k>(n-1)*(n-2)//2:
    print(-1)
else:
    res=[]
    for i in range(2,n+1):
        res+=[(1,i)]
    nbpass=k
    for i in range(2,n+1):
        for j in range(i+1,n+1):
            if nbpass:
                nbpass-=1
            else:
                res+=[(i,j)]
    print(len(res))
    for i in res:
        print(i[0],i[1])

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

if(K<=maxK):
    nokori = maxK-K
    print(N+nokori-1)
    for i in range(N-1):
        print(1, i+2)
    u = 2
    v = 2
    while(nokori != 0):
        v += 1
        if(v==N+1):
            u += 1
            v = u+1
        print(u, v)
        nokori -= 1
else:
    print(-1)

Solution (Python):
from itertools import combinations
N, K = list(map(int, input().split()))

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


if N == 2 and K > 0:
    print("-1")
    exit()

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

max_K = combination_2(N-1)

if K > max_K:
    print("-1")
    exit()

M = max_K - K + N - 1
print(M)
cnt = 0
for x, y in combinations(range(1, N+1), 2):
    print(x, y)
    cnt += 1
    if cnt >= M:
        exit()

Solution (Python):
def solve(n, k):
    if k > (n-1)*(n-2)//2:
        return -1, []

    if k == 0:
        edges = []
        for i in range(n-1):
            for j in range(i+1,n):
                edges.append((i, j))
        return len(edges), edges
    
    edges = []
    for i in range(1, n):
        edges.append((0, i))
    cnt = (n-1)*(n-2)//2

    if k == cnt:
        return len(edges), edges

    for i in range(1, n-1):
        for j in range(i+1,n):
            edges.append((i, j))
            cnt -= 1
            if cnt == k:
                return len(edges), edges

    

if __name__ == "__main__":
    assert solve(5, 8) == (-1, [])


    n,k = map(int, input().split())
    n_edges, edges = solve(n,k)

    print(n_edges)
    
    if n_edges == -1:
        exit()

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

Solution (Python):


from collections import deque
import itertools as ite
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = map(int, raw_input().split())
if K > (N - 1) * N / 2 - (N - 1):
    print -1
    exit()
M = (N - 1) * N / 2 - K
print M
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 < K:
            cnt += 1
        else:
            print i, j

Solution (Python):
import itertools
N, K = map(int, input().split())
maxi = ((N-1)*(N-2))//2
if maxi < K:
    print(-1)
else:
    graph = []
    for i in range(1, N):
        graph.append((i, N))
    delk = list(itertools.combinations([i for i in range(1, N)], 2))
    graph += delk[:maxi-K]
    print(len(graph))
    for i in range(len(graph)):
        print(graph[i][0], graph[i][1])

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

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

if k>mx:
    print("-1")
    exit(0)

cnt = mx - k
print(n-1+cnt)
print(1,2)
for i in range(n-2):
    print(2,i+3)
for i in range(3,n+1):
    for j in range(i+1,n+1):
        if cnt>0:
            print(i,j)
            cnt -= 1
for j in range(3,n+1):
    if cnt>0:
        print(1,j)
        cnt -= 1

Solution (Python):
import itertools

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

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

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

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

dec_left = max_k-K
for i,j in itertools.combinations(list(range(1,N)), 2):
    if dec_left == 0:
        break
    graph_pair_arr.append((i,j))
    dec_left -= 1

print(len(graph_pair_arr))
for i,j in graph_pair_arr:
    print(i+1,j+1)

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

t = 0
L = []
for i in range(1, N - 1):
  t += i
  for j in range(i + 1, N):
    L.append([i + 1, j + 1])

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

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

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

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

    print(i,n)

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

maximum = (n-1)*(n-2)//2
if(k <= maximum):
	
	ans = n - 1 + maximum -k
	print(ans)
	
	for i in range(2, n + 1):
		print("{} {}".format(1 ,i)	)

	
	count = n-1
	if count == ans:
		exit()
	i = 2
	j = 3
	while True:
		print("{} {}".format(i ,j)	)
		count += 1
		if count == ans:
			break
		j += 1
		if j > n:
			i += 1
			j = i+1

else:
	print(-1)

Solution (Python):
N,K = map(int,input().split())
if K == 0:
    ans = []
    for i in range(1,N):
        for j in range(i+1,N+1):
            ans.append((i,j))
    print(len(ans))
    for a,b in ans:
        print(a,b)
    exit()

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

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

rem = mx - K
for i in range(1,N-1):
    for j in range(i+1,N):
        if rem == 0:
            print(len(ans))
            for a,b in ans:
                print(a,b)
            exit()
        ans.append((i,j))
        rem -= 1

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

if k > 0.5*(n-2)*(n-1):
    print(-1)
else:
    print(int(0.5*(n-2)*(n-1) - k + (n-1)))
    hiku = 0.5*(n-2)*(n-1) - k
    for i in range(2,n+1):
        print(1,i)
    a = 1
    b = 0
    while hiku>0 :
        a+=1
        b=a+1
        while hiku>0 and b<=n:
            print(a,b)
            b+=1
            hiku-=1

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

if __name__ == "__main__":
    main()

Solution (Python):
from bisect import bisect_left, bisect_right; from collections import Counter, defaultdict, deque, OrderedDict; from copy import deepcopy; from fractions import gcd; from functools import lru_cache, reduce; from math import ceil, floor; from sys import stdin, setrecursionlimit
import heapq as hq; import itertools as its; import operator as op



inf = float('inf')
input = stdin.readline


def main():
    n, k = get_li()
    if k == 0:
        print(n * (n - 1) // 2)
        for i in range(n):
            for j in range(i + 1, n):
                print(i + 1, j + 1)
        return
    if k > (n-1) * (n-2) // 2:
        print(-1)
        return
    if n == 2:
        print(-1)
        return
    edges = []
    for i in range(1, n):
        edges.append((0, i))
    n_pair = (n-1) * (n-2) // 2
    for i in range(1, n):
        for j in range(i+1, n):
            if n_pair == k:
                break
            edges.append((i, j))
            n_pair -= 1
        if n_pair == k:
            break
    print(len(edges))
    for i, j in edges:
        print(i + 1, j + 1)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


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


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input().strip()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().strip().split(sep=sep))]
        for _ in range(n)
    )))


if __name__ == '__main__':
    setrecursionlimit(4100000)
    main()

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

Solution (Python):
N,K=map(int,input().split())
if 0<=K<=((N-1)*(N-2))//2:
    
    X=[]
    for i in range(1,N):
        X.append((0,i))
    T=((N-1)*(N-2))//2
    for i in range(1,N):
        for j in range(1,N):
            if T>K and i<j:
                X.append((i,j))
                T-=1
    print(len(X))
    for seq in X:
        a,b=seq
        print(a+1,b+1)
else:
    
    print(-1)

Solution (Python):
N, K = map(int, input().split())
if K > (N-1)*(N-2)//2:
    print(-1)
    exit()
A = []
for i in range(1, N):
    A.append([i, N])
k = (N-1)*(N-2)//2
f = False
for i in range(1, N):
    for j in range(i+1, N):
        if k == K:
            f = True
            break
        A.append([i, j])
        k -= 1
    if f:
        break
print(len(A))
for v, u in A:
    print(v, u)

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

if ((N-1) * (N-2)) // 2 < K:
    print(-1)
else:
    ans = [0] * (N-1)
    for i in range(N-1):
        ans[i] = [i+1, N]
    minus = ((N-1) * (N-2)) // 2 - K
    M = N-1
    for i in range(1, N):
        for j in range(i+1, N):
            if minus > 0:
                ans.append([i, j])
                minus -= 1
                M += 1
    print(M)
    for branch in ans:
        print(' '.join(map(str, branch)))

Solution (Python):

import sys
INF = float("inf")


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

    
    M = N-1
    addition = (N-1)*(N-2)//2 - K
    print(M+addition)
    for i in range(2, N+1):     
        print(1, i)

    from_ = 2
    to_ = 3
    for i in range(addition):
        print(from_, to_)
        if to_ < N:
            to_ += 1
        else:
            from_ += 1
            to_ = from_ + 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):
N, K = map(int, input().split())

if ((N-1) * (N-2)) // 2 < K:
    print(-1)
else:
    ans = [0] * (N-1)
    for i in range(N-1):
        ans[i] = [i+1, N]
    minus = ((N-1) * (N-2)) // 2 - K
    M = N-1
    for i in range(1, N):
        for j in range(i+1, N):
            if minus > 0:
                ans.append([i, j])
                minus -= 1
                M += 1
    print(M)
    for branch in ans:
        print(' '.join(map(str, branch)))

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

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

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

print(m)

i=1
while m>0:
    for j in range(i,n):
        print(i,end=' ')
        print(j+1)
        m-=1
        if m==0:
            break
    i+=1

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

if K>X:
  print(-1)
else:
  print(N-1+X-K)
  k=X-K
  
  for i in range(2,N+1):
    print(1,i)
  for a,b in itertools.combinations(range(2,N+1),2):
    if not k:
      break
    print(a,b)
    k-=1

Solution (Python):

import sys























input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())



def iterate_tokens():
    for line in sys.stdin:
        for word in line.split():
            yield word


if __name__ == '__main__':

    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  

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

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

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

if diff < 0:
    print(-1)
    exit()

m = (n - 1) + diff
res = [[1, i] for i in range(2, n + 1)]
i = 2
j = 3

while diff:
    diff -= 1
    res.append([i, j])
    j += 1
    if j > n:
        i += 1
        j = i + 1

print(m)
for x, y in res:
    print(x, y)