Rorbalako $A$.

In the cells of a square table $n \times n$, where $n > 1$, it is required to place different integers from 1 to $n^2$ such that every two consecutive numbers are in adjacent cells, and every two numbers that give the same remainder when divided by $n$ are in different rows and different columns. For which $n$ is this possible?
Let's number the columns and rows from 1 to $n$ respectively from left to right and from top to bottom, and color the board in a checkerboard pattern such that the corner cell in the first column and first row is black.

Let $n$ be even. Fill the table with numbers from 1 to $n^2$ as follows: place them one after another, starting from 1, first in the first row from left to right, and then along the columns: down the last column, up the second-to-last column, and so on (creating something like a snake). In the end, the number $n^2$ will be directly below 1, see the example for $n=6$ in the figure.

| 1 | 2 | 3 | 4 | 5 | 6 |
| :---: | :---: | :---: | :---: | :---: | :---: |
| 36 | 27 | 26 | 17 | 16 | 7 |
| 35 | 28 | 25 | 18 | 15 | 8 |
| 34 | 29 | 24 | 19 | 14 | 9 |
| 33 | 30 | 23 | 20 | 13 | 10 |
| 32 | 31 | 22 | 21 | 12 | 11 |

Now, replace the numbers with their remainders modulo $n$ (see the figure).

| 1 | 2 | 3 | 4 | 5 | 0 |
| :--- | :--- | :--- | :--- | :--- | :--- |
| 0 | 3 | 2 | 5 | 4 | 1 |
| 5 | 4 | 1 | 0 | 3 | 2 |
| 4 | 5 | 0 | 1 | 2 | 3 |
| 3 | 0 | 5 | 2 | 1 | 4 |
| 2 | 1 | 4 | 3 | 0 | 5 |

It is not hard to prove that they are arranged as follows: for an odd column, the last (bottom) number matches the first number of the next even column and the second number of the next odd column. 

This means that each column starts with the remainder $i$, equal to its column number, except for the $n$-th column, which starts with zero. In even columns, the remainders increase from $i$ to $n-1$, and then from 0 to $i+1$. In odd columns, the remainders decrease from $i$ to 0, and then from $n-1$ to $i+1$.

We will prove that in each row all remainders are distinct. Suppose in some row two remainders coincide. They cannot be in columns of the same parity - such columns are cyclic shifts of each other. Therefore, one remainder is in an even column, and the other is in an odd column. But then these two remainders are on cells of different colors and cannot coincide. Contradiction.

Suppose it was possible to fill the table for odd $n$. A contradiction can be reached in different ways.

First method. Notice that in our coloring, black cells are those whose sum of row and column numbers is even, and white cells are the others. In our snake, the colors of the cells alternate, so numbers of the same parity are in black cells, and numbers of the other parity are in white cells.

Consider the cells of the table where numbers giving a remainder $k$ when divided by $n$ are located. The sum of their row and column numbers, by condition, is $(1+2+3+\ldots+n)+(1+2+3+\ldots+n)$, since each row and each column participate once; in particular, this sum is even. But the sum of "coordinates" of each white cell is odd, and of each black cell is even, so the number of white cells among the considered ones is even.

Taking $k=1$ and $k=2$, we get that among the numbers with remainder 1, an even number are on white cells, and among the numbers with remainder 2 - also. But for each number with remainder 1, the next number has remainder 2 and stands on a cell of the opposite color. Therefore, on black cells, an even number of numbers with remainder 2 stand, and the total number of numbers with remainder 2 is even. Contradiction with the oddness of $n$.

Second method. Replace the numbers with their remainders modulo $n$ and draw an arrow from each cell with a 1 to the adjacent cell with a 2. We have $n$ arrows connecting 1s and 2s. Some arrows may be paired - arrows in the opposite direction occupying the same two rows (see the figure). But the number of arrows is odd, so there is an arrow without a pair.

|  |  |  |  |
| :--- | :--- | :--- | :--- |
|  | 1 | $\rightarrow 2$ |  |
|  |  |  |  |
|  |  |  |  |
|  | 2 | 1 |  |
|  |  |  |  |
|  |  |  |  |

Suppose, for example, such an arrow is horizontal and leads from the $i$-th column to the $(i+1)$-th column (see the figure).

|  | $i$ | $i+1$ | $i+2$ | $i+3$ |  |  | $n$ |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
|  |  |  |  | $1-$ | $\rightarrow 2$ |  |  |
|  |  | 1 | $\rightarrow 2$ |  |  |  |  |
|  |  |  |  |  |  |  | 1 |
|  |  |  | 1 | $\rightarrow 2$ |  |  |  |
|  |  |  |  |  |  | $1-$ | $\rightarrow 2$ |
|  | 1 | $\rightarrow 2$ |  |  |  |  |  |

In the $(i+1)$-th column, there is also a 1. Since the first arrow has no pair, the second arrow can only lead to the $(i+2)$-th column (the 2 in the $(i+1)$-th column is already occupied). In the $(i+2)$-th column, there is also a 1, and the arrow from it can only lead to the $(i+3)$-th column (the 2s in the $(i+1)$-th and $(i+2)$-th columns are already occupied). Continuing, we will reach the 1 in the $n$-th column, from where the arrow has nowhere to go. Contradiction.

## Answer

For all even $n$.