Buggy python submission (verdict: Runtime Error):
N, K = tuple(map(int, input().split()))

def new_link_n(i, j):
    s = len(link[i] + link[j])
    for i_link in link[i]:
        if i_link in link[j]:
            s -= 1
        else:
            for j_link in link[j]:
                if i_link in link[j_link]:
                    s -= 1
                    break
    for j_link in link[j]:
        if j_link in link[i]:
            s -= 1
        else:
            for i_link in link[i]:
                if j_link in link[i_link]:
                    s -= 1
                    break
    return s

# Kを減らす
def make_link1(M, s):
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            if j in link[i]:
                continue
            n = new_link_n(i, j)
            if n <= 0 and s + n >= K:
                link[i].append(j)
                link[j].append(i)
                s += n
                M += 1
                if s == K:
                    return M
                return make_link1(M, s)

# Kを増やす
def make_link2(M, s):
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            if j in link[i]:
                continue
            n = new_link_n(i, j)
            if n >= 0 and s + n <= K:
                link[i].append(j)
                link[j].append(i)
                s += n
                M += 1
                if s == K:
                    return M
                return make_link2(M, s)

        
if K > (N - 1) * (N - 2) / 2:
    print(-1)
else:
    link = {i: [] for i in range(1, N+1)}
    for i in range(1, N):
        link[i].append(i + 1)
        link[i + 1].append(i)
    M = N - 1
    now_k = N - 2
    if now_k == K:
        pass
    elif now_k > K:
        M = make_link1(M, now_k)
    else:
        M = make_link2(M, now_k)
    print(M)
    for i in range(1, N+1):
        for j in link[i]:
            if i < j:
                print(i, j)

Fixed submission:
N, K = tuple(map(int, input().split()))

def make_link(M, s):
    for i in range(2, N+1):
        for j in range(i+1, N+1):
            link[i].append(j)
            M += 1
            s -= 1
            if s == K:
                return M

max_link = (N - 1) * (N - 2) / 2
if K > max_link:
    print(-1)
else:
    link = {i: [] for i in range(1, N+1)}
    for i in range(2, N+1):
        link[1].append(i)
    M = N - 1
    if max_link == K:
        pass
    else:
        M = make_link(M, max_link)
    print(M)
    for i in range(1, N+1):
        for j in link[i]:
            print(i, j)

Buggy python submission (verdict: Runtime Error):
N, K = tuple(map(int, input().split()))

def new_link_n(i, j):
    s = len(link[i] + link[j])
    for i_link in link[i]:
        if i_link in link[j]:
            s -= 1
        else:
            for j_link in link[j]:
                if i_link in link[j_link]:
                    s -= 1
                    break
    for j_link in link[j]:
        if j_link in link[i]:
            s -= 1
        else:
            for i_link in link[i]:
                if j_link in link[i_link]:
                    s -= 1
                    break
    return s

# Kを減らす
def make_link1(M, s):
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            if j in link[i]:
                continue
            n = new_link_n(i, j)
            if n <= 0 and s + n >= K:
                link[i].append(j)
                link[j].append(i)
                s += n
                M += 1
                if s == K:
                    return M
                return make_link1(M, s)

# Kを増やす
def make_link2(M, s):
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            if j in link[i]:
                continue
            n = new_link_n(i, j)
            if n >= 0 and s + n <= K:
                link[i].append(j)
                link[j].append(i)
                s += n
                M += 1
                if s == K:
                    return M
                return make_link2(M, s)

        
if K > sum([i for i in range(1, N-1)]):
    print(-1)
else:
    link = {i: [] for i in range(1, N+1)}
    for i in range(1, N):
        link[i].append(i + 1)
        link[i + 1].append(i)
    M = N - 1
    now_k = N - 2
    if now_k == K:
        pass
    elif now_k > K:
        M = make_link1(M, now_k)
    else:
        M = make_link2(M, now_k)
    print(M)
    for i in range(1, N+1):
        for j in link[i]:
            if i < j:
                print(i, j)

Fixed submission:
N, K = tuple(map(int, input().split()))

def make_link(M, s):
    for i in range(2, N+1):
        for j in range(i+1, N+1):
            link[i].append(j)
            M += 1
            s -= 1
            if s == K:
                return M

max_link = (N - 1) * (N - 2) / 2
if K > max_link:
    print(-1)
else:
    link = {i: [] for i in range(1, N+1)}
    for i in range(2, N+1):
        link[1].append(i)
    M = N - 1
    if max_link == K:
        pass
    else:
        M = make_link(M, max_link)
    print(M)
    for i in range(1, N+1):
        for j in link[i]:
            print(i, j)