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

Solution (Python):
import sys

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

def main():
    r = (n - 1) * (n - 2) // 2 - k
    if r < 0:
        return [[-1]]

    edges = [(1, v) for v in range(2, n+1)]
    m = n - 1 + r

    u = 2; v = 3
    for _ in range(r):
        edges.append((u, v))
        v += 1
        if v == n + 1:
            u += 1
            v = u + 1

    res = [[m]] + edges 
    return res
    
if __name__ == '__main__':
    ans = main()
    for a in ans:
        print(*a, sep=' ')

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

if K > (N - 1) * (N - 2) // 2:
    print(-1)
else:
    M = N * (N - 1) // 2 - K
    cnt = 0
    ans = []
    for i in range(N - 1):
        for j in range(i + 1, N):
            ans.append((i + 1, j + 1))
            cnt += 1
            if M == cnt:
                break
        else:
            continue
        break
    print(M)
    for edge in ans:
        print(*edge)

Solution (Python):
import sys
import itertools

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


N, K = lr()
max = (N-1) * (N-2) // 2
if K > max:
    print(-1)
    exit()
minus = max - K
print(N - 1 + minus) 

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

leaf_com = []
for x in itertools.combinations(range(2, N+1), 2):
    leaf_com.append(x)

for i in range(minus):
    u, v = leaf_com[i]
    print(u, v)

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

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

    def out(x):
        cnt = 0
        for i in range(2, N+1):
            for j in range(i+1, N+1):
                if cnt == amax-K:
                    return
                print(i, j)
                cnt += 1
    out(amax-K)
else:
    print(-1)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,k = inpl()

if k > (n-1)*(n-2)//2:
    print(-1)
    quit()
res = []
for i in range(n-1):
    res.append([1,2+i])
l = (n-1)*(n-2)//2 - k

ind = 2

def p():
    m = len(res)
    print(m)
    for i in range(m):
        print(*res[i])
if l == 0 :
    p(); quit()
for i in range(ind,n):
    for j in range(i+1,n+1):
        res.append([i,j])
        l -= 1
        if l == 0:
            p()
            quit()
    ind += 1

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

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

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

edge = []
add = int(mx - k)
for i in range(n - 1):
    for j in range(i):
        edge.append([i + 1, j + 1])

for i in range(add):
    ans.append(edge[i])

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

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

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

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

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

    i = 2
    while K < n and i < N:
        j = i + 1
        while K < n and j < N + 1:
            graph.append((i, j))
            n -= 1
            j += 1
        i += 1

    print(len(graph))
    for p, v in graph:
        print(p, v)


main()

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


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

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

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

add_edge = mx - k
edge = []
for i in range(1, n-1):
    for j in range(i+1, n):
        edge.append((i, j))

for i in range(add_edge):
    ans.append(edge[i])

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

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n,k=map(int,input().split())
G=[]
for i in range(2,n+1):
    G.append((1,i))
s=(n-1)*(n-2)//2
if k > s:
    print(-1); exit()
for i in range(2,n):
    for j in range(i+1,n+1):
        if count < s-k:
            G.append((i,j))
            count += 1

print(len(G))
for gi in G:
    print(*gi)

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

if 2 * k > (n-1) * (n-2):
  print(-1)
else:
  ans = [(1, i) for i in range(2, n+1)]
  p =  (n-1) * (n-2) // 2
  i = 2
  j = 3
  while p > k:
    ans.append((i, j))
    p -= 1
    j += 1
    if j == n + 1:
      i += 1
      j = i + 1
  print(len(ans))
  for a in ans:
    print(" ".join(map(str, a)))

Solution (Python):
import itertools
from sys import exit

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

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

cnt = max_edge
it = itertools.combinations(range(1, N), 2)
while cnt > K:
    print(*next(it))
    cnt -= 1

Solution (Python):
import sys

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

ans = []

for i in range(N-1):

    i += 2

    ans.append([1,i])

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

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

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

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

    for j in range(i+1,N+1,1):

        if nsum == K:
            print (len(ans))
            for x,y in ans:
                print (x,y)
            sys.exit()

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

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

Solution (Python):
from math import factorial
from itertools import combinations, islice

def nCr(n, r):
    return factorial(n) // factorial(r) // factorial(n - r)

def main():
    N, K = map(int, input().split())
    A = nCr(N - 1, 2) if N > 2 else 0
    if K > A:
        print(-1)
        return
    print(N - 1 + A - K)
    for i in range(2, N + 1):
        print(1, i)
    for u, v in islice(combinations(range(2, N + 1), 2), A - K):
        print(u, v)

main()

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

if K<=(N-1)*(N-2)/2:
  E = []
  for i in range(N-1):
    for j in range(i):
      E += [(j, i)]
  E = E[:(N-1)*(N-2)/2-K]
  for i in range(N-1):
    E += [(i, N-1)]
  print len(E)
  for e in E:
    print e[0]+1, e[1]+1
else:
  print "-1"

Solution (Python):
def main():
    N, K = map(int, input().split())
    mx = (N - 1) * (N - 2) // 2
    if mx < K:
        print(-1)
        exit()
    ans = []

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

    cnt = mx - K
    for j in range(2, N+1):
        for k in range(j,N+1):
            if cnt == 0:
                break
            if j == k:
                continue
            ans.append((j,k))
            cnt -= 1

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



    



if __name__ == "__main__":
    main()

Solution (Python):
from collections import *
from itertools import *
import copy
from heapq import *


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

count=(N-1)*(N-2)//2-K
relst=[]
lst=[i for i in range(1,N)]
for l in lst:
    relst.append([l,N])


for l1 in range(1,N):
    for l2 in range(l1+1,N):
        if count>0:
            relst.append([l1,l2])
            count-=1
        else:
            break
print(len(relst))
for a,b in relst:
    print(a,b)

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

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

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

Solution (Python):

import sys
from itertools import combinations


def make_deletables(n):
    return list(combinations(range(1, n), 2))


def delete_2len(edges, n, n_delete):
    candidates = make_deletables(n)
    for k, v in candidates[:n_delete]:
        edges[k].append(v)
    return edges


def make_ball(n):
    return {i: [n] for i in range(1, n)}


def is_possible(n, k):
    return k <= ((n - 1) * (n - 2) // 2)


def calc_n_delete(n, k):
    return ((n - 1) * (n - 2) // 2) - k


def list_dict_to_tpls(dic):
    tpls = []
    for k, lis in dic.items():
        tpls += [(k, l) for l in lis]
    return tpls


def solve(N: int, K: int):
    if not is_possible(N, K):
        print(-1)
        return
    edges_dict = make_ball(N)
    n_delete = calc_n_delete(N, K)
    edges_dict = delete_2len(edges_dict, N, n_delete)
    edges_tpls = list_dict_to_tpls(edges_dict) 
    print(len(edges_tpls))
    for i, j in edges_tpls:
        print(i, j)
    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):
from itertools import combinations
N,K=map(int,input().split())
M=(N-1)*(N-2)//2
if K>M:
  print(-1);exit()

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

for i,a in enumerate( list(combinations(range(2, N+1), 2)) ):
  if i==M-K:
    break
  print(*a)

Solution (Python):
from itertools import combinations

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

K_max = (N - 1) * (N - 2) // 2
if K > K_max:
    print(-1)
else:
    surplus = K_max - K
    edges = [(i + 1, N) for i in range(N - 1)] + \
        list(combinations(list(range(1, N)), 2))[:surplus]
    print(len(edges))
    for edge in edges:
        print(edge[0], edge[1])

Solution (Python):
from itertools import combinations


a,b = map(int,input().split())

S  = (a-1)*(a-2)//2
def solve(x,y,s):
    print(a+s-y-1)
    for i in range(1,a):print(1,1+i)
    c = s-b
    edges = list(combinations(range(2, a+1), 2))[:c]
    for i, j in edges:
        print(i, j)

if not 0<= b <= S:
    print(-1)
    exit()
else:
    solve(a,b,S)

Solution (Python):
a,b = map(int,input().split())

S  = (a-1)*(a-2)//2
def solve(x,y,s):
    print(a+s-y-1)
    for i in range(1,a):print(1,1+i)
    c = s-b
    for i in range(1,a):
        for I in range(i+1,a):
            if c > 0:
                print(i+1,I+1)
                c-=1
            else:exit()

if not 0<= b <= S:
    print(-1)
    exit()
else:
    solve(a,b,S)

Solution (Python):
a,b = map(int,input().split())

S  = (a-1)*(a-2)//2
def solve(x,y,s):
    print(a+s-y-1)
    for i in range(1,a):
        print(1,1+i)
    c = 0
    for i in range(1,a):
        for I in range(i+1,a):
            if c == s-b:exit()
            print(i+1,I+1)
            c+=1
            if c == s-b:exit()

if not 0<= b <= S:
    print(-1)
    exit()
else:
    solve(a,b,S)

Solution (Python):
a,b = map(int,input().split())

S  = (a-1)*(a-2)//2
def solve(x,y,s):
    print(a+s-y-1)
    for i in range(1,a):
        print(1,1+i)
    c = s-b
    for i in range(1,a):
        for I in range(i+1,a):
            if c > 0:
                print(i+1,I+1)
                c-=1
            else:exit()

if not 0<= b <= S:
    print(-1)
    exit()
else:
    solve(a,b,S)

Solution (Python):
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor


"""

"""

N = gi()
K = gi()


def run():
    M = N - 1
    d = ((N - 1) * (N - 2)) // 2 - K
    M += d

    if M < (N - 1):
        print(-1)
        return

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

    for v in range(2, N + 1):
        for u in range(v + 1, N + 1):
            if not d:
                return
            d -= 1
            print(v, u)


run()

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

edges = [(0, i) for i in range(1, n)]
cnt = (n-1) * (n-2) // 2

if k == cnt:
    print(n-1)
    for u, v in edges:
        print(u+1, v+1)
    exit()

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

print(-1)

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

if M >= K:
    print(M-K+N-1)
    for i in range(1,N):
        print(1,1+i)
    tmp = 0
    for i in range(2,N):
        for j in range(i+1,N+1):
            if tmp == M-K:
                break
            print(i,j)
            tmp += 1
else:
    print(-1)

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

    for j in range(2,n+1):
        for K in range(j+1,n+1):
            lists.append((j,K))
    lists=lists[:n-1+P]
    print(len(lists))
    for some in lists:
        print(some[0],some[1])

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

upper = (N - 1) * (N - 2) // 2
if K <= upper:
    pair = [(1, i) for i in range(2, N + 1)]
    rest = upper - K

    edge = []
    for i in range(2, N):
        for j in range(i + 1, N + 1):
            edge.append((i, j))
    pair.extend(edge[:rest])

    print(len(pair))
    for p in pair:
        print(*p)
else:
    print(-1)

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

Solution (Python):
import itertools

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

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

if m2>=0:
    M=N-1+m2
    print(M)
    for i in range(1,N):
        print(1,i+1)
    i=0
    for a,b in  itertools.combinations(range(1,N), 2):
        if i==m2:
            break
        print(a+1,b+1)
        i+=1
        
else:
    print(-1)

Solution (Python):
import sys

input = sys.stdin.readline


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

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

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

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

        star += leaves[:max_pair - K]
        print(len(star))
        for pair in star:
            print(" ".join(map(str, pair)))



if __name__ == '__main__':
    AtCoder().main()

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

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

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

if K>(N-1)*(N-2)//2:
    print(-1)
    exit()
    
edges = []

for i in range(1, N):
    edges.append((i, N))
    
cnt = 0

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

print(len(edges))

for ui, vi in edges:
    print(ui, vi)

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

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

if n < 0:
  print(-1)
else:
  M = N-1 + n
  print(M)
  for i in range(2, N+1):
    print(1, i)
  
  if n > 0:
    count = 0
    for i in range(2, N+1):
      for j in range(i+1, N+1):
        print(i, j)
        count += 1
        if count == n:
          break

      if count == n:
        break

Solution (Python):
def solve():
    N, K = map(int, input().split())
    
    m_max = (N - 1) * (N - 2) // 2
    if m_max < K:
        print(-1)
        exit()
    
    M = 0
    graph = [[] for _ in range(N + 1)]
    for i in range(2, N + 1):
        graph[1].append(i)
        M += 1
    
    cnt = m_max - K
    for i in range(2, N):
        for j in range(i + 1, N + 1):
            if cnt == 0:
                break
            graph[i].append(j)
            cnt -= 1
            M += 1
        else:
            continue
        break
    
    print(M)
    for parent in range(1, N + 1):
        for child in graph[parent]:
            print(parent, child)


if __name__ == '__main__':
    solve()

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

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

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

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

for i, (v1, v2) in enumerate(combinations(range(2, n+1), 2)):
    if i == n_add:
        break
    print(v1, v2)

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

for i in range(2,N+1):
    print(1,i)
    c += 1
    if c >= M:
        exit()
for i in range(2,N):
    for j in range(i+1,N+1):
        print(i,j)
        c += 1
        if c >= M:
            exit()

Solution (Python):
import sys

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))  
rs = lambda: stdin.readline().rstrip()  

N, K = rl()
max = (N-1) * (N-2) // 2
if K > max:
    print(-1)
    exit()
edges = []
for i in range(2, N+1):
    edges.append((1, i))
count = max - K
for i in range(2, N):
    if count == 0:
        break
    for j in range(i+1, N+1):
        if count == 0:
            break
        edges.append((i, j))
        count -= 1
print(len(edges))
for x in edges:
    print(*x)

Solution (Python):
import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n, k = list(map(int, input().rstrip('\n').split()))
    if k <= (n - 1) * (n - 2) // 2:
        t = n - 1 + ((n - 1) * (n - 2)) // 2 - k
        print(t)
        cnt = 0
        for i in range(2, n + 1):
            print(1, i)
            cnt += 1
        for i in range(2, n + 1):
            for j in range(i + 1, n + 1):
                if cnt == t:
                    exit()
                print(i, j)
                cnt += 1
    else:
        print(-1)


if __name__ == '__main__':
    solve()

Solution (Python):
def s():
	n, k = map(int, input().split())
	ans = ["1 {}".format(i) for i in range(2, n + 1)]
	c = (n - 1) * (n - 2) // 2
	if k > c:
		print(-1)
		exit()
	for i in range(2, n):
		for j in range(i + 1, n + 1):
			if c == k:
				break
			ans += ["{} {}".format(i, j)]
			c -= 1
		else:
			continue
		break
	print(len(ans))
	print(*ans)

if __name__=="__main__":
	s()

Solution (Python):
n,k=map(int,input().split())
ans=["1 "+str(i) for i in range(2,n+1)]
c=(n-1)*(n-2)//2
if k>c:
	print(-1)
	exit()
for i in range(2,n):
	for j in range(i+1,n+1):
		if c==k:
			break
		ans+=["{} {}".format(i,j)]
		c-=1
	else:
		continue
	break
print(len(ans))
print(*ans,sep="\n")

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

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

if k < K:
  print(-1)
  exit()
  
answer = []
for i in range(2, N+1):
  answer.append([1, i])


for i in range(2, N+1):
  for j in range(i+1, N+1):
    if k == K:
      print(len(answer))
      for a in answer:
        print(*a)
      exit()
    else:
      k -= 1
      answer.append([i, j])
print(len(answer))
for a in answer:
  print(*a)
exit()

Solution (Python):

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

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

nums = (N-1)*(N-2)//2 - K
for i in range(N-1):
    for j in range(i+1,N-1):
        if nums==0:break
        edge.append([i+1,j+1])
        nums-=1
    if nums==0:break

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

Solution (Python):
def examA():
    S = SI()
    ans = "Good"
    for i in range(len(S)-1):
        if S[i+1]==S[i]:
            ans = "Bad"
    print(ans)
    return

def examB():
    N, L = LI()
    ans = 0
    if L>=0:
        for i in range(N - 1):
            ans += (L + i + 1)
    else:
        for i in range(N):
            ans += (L+i)
        if L+N-1<0:
            ans -=(L+N-1)
    print(ans)
    return

def gcd(x, y):
    if y == 0:
        return x
    while y != 0:
        x, y = y, x % y
    return x
def lcm(x, y):
    return x * y // gcd(x, y)
def examC():
    A, B, C, D = LI()
    CD = lcm(C,D)
    ans = B - (B//C + B//D - B//CD) - ((A-1) - ((A-1)//C + (A-1)//D - (A-1)//CD))
    print(ans)
    return

def examD():
    N = I()
    AB = [LI() for _ in range(N)]
    AB.sort(key=lambda x:x[1])
    cur = 0; ans = "Yes"
    for a,b in AB:
        cur +=a
        if b<cur:
            ans = "No"
            break
    print(ans)
    return

def examE():
    N, K = LI()
    maxK = (N-1)*(N-2)//2
    if K>maxK:
        print(-1)
        exit()
    ansC = []
    for i in range(1,N):
        ansC.append([1,i+1])
    k = maxK-K
    for i in range(1, N):
        if k == 0:
            break
        for j in range(i + 1, N):
            ansC.append([i+1, j+1])
            k -= 1
            if k == 0:
                break
    M = len(ansC)
    print(M)
    for v in ansC:
        print(" ".join(map(str,v)))
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examE()

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

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

MAXK = int((N-1)*(N-2)/2)

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

edge = []


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

now = 2
node = now + 1
for _ in range(MAXK - K):
    edge.append([now,node])
    node += 1
    if node > N:
        now += 1
        node = now + 1

M = len(edge)
print(M)
for u,v in edge:
    print(u,v)

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


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

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


def f():
    global now_K
    for i in range(1, N):
        for j in range(i+1, N):
            if now_K == K:
                break
            ans.append((i+1, j+1))
            now_K -= 1
f()
print(len(ans))
for a, b in ans:
    print(a, b)

Solution (Python):
N,K=map(int,input().split())
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
if N==2 and K>=1:
    print(-1)
    exit()
if K>cmb(N-1,2):
    print(-1)
else:
    count=cmb(N-1,2)-K
    print(cmb(N,2)-K)
    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 count==0:
                exit()
            print(i,j)
            count-=1

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

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n,k=map(int,input().split())
    r=(n-1)*(n-2)//2-k 
    if(r<0):
        print(-1)
        return

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

    
    if(r==0): return
    from itertools import product
    for i,j in product(range(2,n+1),repeat=2):
        if(i>=j): continue 
        print(i,j)
        r-=1
        if(r==0): return
resolve()

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

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

s,g=2,3
while k!=K:
    G.append([s,g])
    g+=1
    if g==N+1:
        s+=1
        g=s+1
    k-=1

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

Solution (Python):
import  sys
from itertools import combinations
n, k = [int(i) for i in sys.stdin.readline().split()]
if (n - 1) * (n - 2) // 2 < k:
    print(-1)
else:
    print(n - 1 + (n - 1) * (n - 2) // 2 - k)
    for i in range(2,n+1):
        print(1, i)
    cnt = 0
    for combi in combinations(range(2,n+1), 2):
        if (n - 1) * (n - 2) // 2 - k == cnt:
            break
        print(combi[0], combi[1])
        cnt += 1

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

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

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

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

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

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

max_num = (N-2)*(N-2+1)/2
M = 0
if max_num >=K:
    base_graph = [[] for _ in range(N+1)]
    base_graph[1].append(2)
    base_graph[2].append(1)
    for i in range(3,N+1):
        base_graph[1].append(i)
        base_graph[i].append(1)
    
    M = N- 1

    idx = N
    diff = max_num-K
    while diff != 0:
        if len(base_graph[idx]) == N-1:
            idx -= 1
        else:
            insert_val = len(base_graph[idx]) + 1 - (N-idx)
            base_graph[idx].append(insert_val)
            base_graph[insert_val].append(idx)
            diff -=1
            M +=1
    print(M)
    for idx,node in enumerate(base_graph): 
        for i in node:
            if idx < i:
                print(idx,i)
        
else:
    print(-1)

Solution (Python):
from itertools import combinations

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


def cmb(x):
    return x * (x - 1) // 2


if K > cmb(N - 1):
    print(-1)
else:
    m = cmb(N - 1) - K
    print(N - 1 + m)

    for i, j in list(combinations(range(1, N + 1), 2))[:N - 1 + m]:
        print(i, j)

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

edges = [[x for x in range(n) if x > i] for i in range(n)]
count = 0
node = n-1
while count != k:
    if len(edges[node]) == 0:
        node -= 1
    edges[node].pop()
    count += 1

m = n * (n-1) // 2 - k
print(m)
for _from in range(n-1):
    for to in edges[_from]:
        print(_from+1, to+1)