Under construction

Still being built. Things may change

Vera and Dogs

Time limit2sMemory limit512 MB

Summary
Construct an assignment of primary and secondary doghouses to M = X*N dogs so that closing any single doghouse keeps every open doghouse at or below X+1 sleepers.
Level

Medium7 of 10

Topics
Combinatorics, Greedy, Math, Implementation
Solved
No attempts yet

Problem

Vera has NN doghouses numbered 11 to NN and MM dogs numbered 11 to MM, where M=X×NM = X \times N. Doghouse ii must be the primary home of exactly XX dogs Pi,1,…,Pi,XP_{i,1}, \dots, P_{i,X} and the secondary home of exactly XX dogs Si,1,…,Si,XS_{i,1}, \dots, S_{i,X}. Every dog has one primary home and one secondary home, and the two are different doghouses.

Every night at most one doghouse is closed for cleaning. A dog sleeps in its primary home when that home is open, and in its secondary home otherwise. On every possible night, including the night when no doghouse is closed, each open doghouse must hold at most X+1X + 1 sleeping dogs.

Find an assignment that meets all of these conditions, or report that none exists.

Input

The first line contains two integers NN and XX (1≤N,X≤20171 \le N, X \le 2017, X×N≤50000X \times N \le 50000).

Output

If no valid assignment exists, print -1 on a single line.

Otherwise print NN lines. Line ii contains 2X2X integers: first the XX dogs whose primary home is doghouse ii, in increasing order of dog number, then the XX dogs whose secondary home is doghouse ii, in increasing order of dog number.

Several assignments can satisfy the conditions, so only this one counts as correct. For every ii from 11 to NN and every kk from 11 to XX, the dog numbered (i−1)X+k(i - 1)X + k has doghouse ii as its primary home and doghouse ((i+k−1) mod N)+1((i + k - 1) \bmod N) + 1 as its secondary home.

Notes

Doghouse numbers and dog numbers both start at 11. The night on which no doghouse is closed also has to satisfy the capacity rule, and on that night each doghouse holds exactly the XX dogs that call it their primary home.

Examples2

  1. Example 1

    Input
    3 2
    
    Expected output
    1 2 4 5
    3 4 1 6
    5 6 2 3
    
  2. Example 2

    Input
    2 2
    
    Expected output
    -1