The arrangement that takes longest to untangle is most likely the “mirror” — the ring reflected through the swap window — and it needs as many moves as there are pairs of balls: n(n−1)/2. Brute force up to 15 balls, a neural network with beam search up to 100 and an open competition up to 200 balls all agree, and proofs squeeze the answer into a narrow fork.
Video: the same story in 11 minutes
The article follows the video scene by scene, in more detail. Each diagram links to its moment in the video.
Paper: CayleyPy RL: Pathfinding and Reinforcement Learning on Cayley Graphs, arXiv 2502.18663, 25 February 2025; Advances in Theoretical and Mathematical Physics, 2026. The ring, floors, ceiling, compass and front are images for explanation; in the paper they are a Cayley graph, distance, diameter, a neural network and beam search. The video is in Russian.
Question: a ring, three moves and the most tangled arrangement
Take a ring of balls, each with a number. However you shuffle them, you can always put them back in order. The question is which arrangement takes longest to untangle and how many moves it needs.
Three words will matter. A move is turning the ring left, turning it right, or swapping the two balls in the window at the top. An arrangement is how the balls sit around the ring. There are no other moves: you can't pick a ball from the middle, everything goes through the window.
Left: balls in order; center: after a left turn; right: after a swap in the window. ▶ 0:08
Why is it hard? Untangling any arrangement is easy: bring the needed ball to the window and swap until everything is in place. But that wastes many moves. To know the minimum you have to compare all paths, and a ring has 1·2·3·…·n arrangements, a number that outgrows any power.
A related problem — the same ring, turning only one way plus the swap — was posed by the cybernetician Viktor Glushkov in 1968. It is still open.
The paper, its authors and the competition
The question is studied by 34 authors of the CayleyPy project — the same team that taught a neural network to solve 4×4×4 and 5×5×5 Rubik's Cubes.
“CayleyPy RL” is the second paper of the series: the first showed the method on cubes, this one carries it over to general Cayley graphs and tests it on the ring of balls. Four ways to answer one question meet in it: brute force on small rings, a trained program for large ones, an open programming competition and rigorous proofs.
34 authors in paper order, names as printed in the paper.
Kaggle is a platform where researchers post problems and teams from all over the world compete on solutions. The authors opened three challenges about this ring there and published their code so anyone can run it and try to beat it.
The tower of arrangements: floors and the ceiling
Let's draw all arrangements at once. Five balls have 120 of them.
Put each arrangement on a floor: how many moves it is from order along the shortest path. Order is at the bottom, three neighbors on the first floor (one per move), then floors widen and narrow again. The top floor is the ceiling: that many moves are enough for any arrangement. For five balls the ceiling is floor 10, and exactly one arrangement sits there.
Bar width is the number of arrangements on the floor; computed by brute force. ▶ 1:13
The ceiling is the same thing as the God's number of the Rubik's Cube: the largest number of moves the shortest solution may need. In math it is the graph diameter. If every arrangement's floor were known, untangling would be easy: always step one floor down.
- 1FloorHow many moves from the arrangement to order along the shortest path
- 2CeilingThe top floor: enough moves for any arrangement; God's number, the graph diameter
- 3QuestionWho sits on the ceiling and how high it is for any number of balls
Who sits on the ceiling: the mirror
The authors ran a brute-force search over every arrangement of rings up to 15 balls. For 15 that is over a trillion arrangements and several days on a cluster.
Every time, exactly one arrangement sits at the top — the mirror: the ring reflected through the window. The two balls in the window swap, and the rest swap in pairs across the axis. Surprisingly, simply putting the balls in reverse order lands two floors lower.
Left: order; center: the mirror (dashed — the axis through the window); right: reversed. ▶ 1:52
Bar length is the number of digits in the arrangement count. ▶ 1:58
The ceiling equals the number of pairs
Five balls form pairs along ten lines, and the ceiling for five balls is also 10. Six balls: 15 pairs and a ceiling of 15.
Each line is one pair of balls. ▶ 2:30
It matches on every ring the brute force covered, starting from four balls; three balls have a ceiling of 2 but 3 pairs. Hence the conjecture from the Online Encyclopedia of Integer Sequences (OEIS A186783): the ceiling always equals the number of pairs, n(n−1)/2.
| Balls | Pairs | Ceiling by brute force |
|---|---|---|
| 4 | 6 | 6 |
| 5 | 10 | 10 |
| 6 | 15 | 15 |
| 10 | 45 | 45 |
| 15 | 105 | 105 |
Twenty balls: brute force can't reach
Twenty balls already have 2.4·10¹⁸ arrangements — billions of billions. So the conjecture is tested differently: take the mirror and look for a shorter way to untangle it.
If the path is never shorter than the number of pairs, the conjecture holds. First, the classic computer algebra program for groups (GAP). It always finds some path but doesn't care about its length: on a 20-ball ring it works for 41 minutes and returns 217,944 moves. There are only 190 pairs.
Top: the number of pairs; bottom: the classic program's path. ▶ 3:00
The compass from the previous video
The authors reuse the trick from the Rubik's Cube: a compass trained on random walks.
First, walks: from order, make random moves and after each one record the arrangement and how many moves were made. No map of all arrangements is needed, and walks cost almost nothing. A compass learns from these records — a neural network that looks at an arrangement and guesses its floor.
The compass is an ordinary neural network: an arrangement in, a number out.
The compass guesses with an error: a walk loops, so it has more moves than the shortest path. But the farther a walk went, the higher the floor on average, so the compass points the right way: where the number is smaller, order is closer.
The walk from the video: grey — the walk's step, blue — the true floor. ▶ 3:42
The front: many arrangements at once
Compass errors lead a single path into a dead end. So the search moves as a front — beam search in the paper.
A front is several arrangements at once. At each step all three moves are tried from each of them, the compass scores the new neighbors, and only the most promising stay for the next step. If the compass is wrong about one arrangement, there are others in the front, and the path doesn't break.
On 20 balls this finds a path of exactly 190 moves — in 3 minutes 48 seconds instead of 41 minutes. Up to 28 balls the path equals the number of pairs; up to 44 it is found but longer.
Bar height is the found path divided by the number of pairs; the line is exactly the pairs. Tables 2–3 of the paper. ▶ 4:18
A one-line hint
The authors added one line to the search: don't swap the balls in the window if they are already in order.
The idea is simple: swapping a pair that is already ordered almost never pays off, while the extra option bloats the front. That line pushed the limit from 40 balls to 100. For 100 balls a path is found in 9 hours, although it is still more than three times longer than the number of pairs: 16,632 moves against 4,950 pairs. And the hint is tailored to this ring: for other problems it is yet to be found.
Left: the window where a swap is forbidden; right: how many balls can be untangled. ▶ 4:54
An open competition
The authors invited everyone to untangle the mirror: an open programming competition on Kaggle, rings from 2 to 200 balls.
Participants reached paths of exactly the number of pairs. Nobody found a shorter one — by program or by hand.
Rings from 2 to 200 balls; a check means a path of exactly the pairs. ▶ 5:27
Why it can't be shorter: triples
Take any three balls. Around the ring they run either clockwise or counterclockwise.
Turning the ring doesn't flip a triple: all balls move together and the order around is kept. Only a swap in the window can flip it, and only when two of its balls are in the window. And the mirror flips all triples at once. From this the authors proved that the mirror needs almost as many moves as there are pairs: at most half the number of balls plus one move fewer.
The triple 1, 2, 4: left as is, center after a turn, right after a swap. ▶ 5:51
The fork: proven bounds
From above, the ceiling is bounded by the authors' untangling method: any arrangement is untangled in at most the number of pairs plus three moves per ball.
That gives a fork. For 20 balls the ceiling lies between 179 and 250, while the conjecture says 190. The more balls, the narrower the fork relative to the ceiling itself: for 100 balls it spans a few percent of the number of pairs.
The bar is the proven bounds, the line is the conjecture. ▶ 6:29
Takeaway
The question was: which arrangement takes longest to untangle and how many moves does it need? Most likely the mirror, with as many moves as there are pairs of balls.
After the paper, one of its authors, Grigorii Antiufeev, posted a proof that the mirror cannot be untangled faster than the number of pairs (arXiv 2601.08715, a 2026 preprint). One thing remains open: whether some arrangement sits even farther away.
Caveat. “Ceiling = number of pairs” is a conjecture: only the bounds are proven. The lower bound of exactly the number of pairs is a preprint not yet peer-reviewed.
Formally: the same path in the paper's language
The second part of the video: the same steps as formulas.
| In the video | In the paper | How it works |
|---|---|---|
| ring with all arrangements | LRX Cayley graph of the symmetric group Sₙ | vertices are permutations, edges are moves |
| moves | generators L, R, X | L — left shift, R = L⁻¹, X = (0, 1) — swap of the first two positions |
| ceiling | graph diameter | conjecture: n(n−1)/2 for n ≥ 4 |
| mirror | longest element ℓₙ | (1 0 n−1 … 3 2) = R² · (reversed order) |
| compass | network on diffusion distance + Q-learning | first learns from walks, then is checked against the Bellman equation d(s) = 1 + min d(t) over neighbors t |
| front | beam search | width up to 2²⁰; the hint is the X-trick |
| fork | propositions 4.2 and 4.3 | n(n−1)/2 − n/2 − 1 ≤ diameter ≤ n(n−1)/2 + 3n |
The paper's novelty is how the compass is trained. Walks give a first guess, then the compass is checked against its neighbors: one move more to order than from the nearest neighbor. Like the way home: if your neighbor is five minutes away, you are at most six. On a 28-ball ring this cut the typical path from 650 moves to 402, with 378 pairs.
def beam_search(start, model, W, steps=1000):
front = [start]
for _ in range(steps):
nbrs = {m(s) for s in front for m in (L, R, X)
if not (m is X and s[0] < s[1])} # the hint
if SORTED in nbrs: return True
front = sorted(nbrs, key=model)[:W] # W best by the compass
return FalseA sketch of beam search with the hint; the full code is on Kaggle. ▶ 9:46
Where it helps
The compass and the front don't care that it's a ring: they need only states, moves and a goal. So the same approach fits any task of getting from one state to another along the shortest path.
| Link | Where |
|---|---|
| direct | the TopSpin (Oval Track) and Hungarian Rings puzzles work almost the same way |
| direct | processor networks: the diameter of the link graph is the longest message delay |
| direct | shortest paths in huge permutation graphs where brute force is impossible |
| analogy | planning: getting from one state to another in small steps |
Lesson. Brute force gives a conjecture, a trained program tests it far beyond brute force, and a proof squeezes the answer into a fork.
Who and when, what is proven
- 11968Viktor Glushkov: the ring problem with two moves
- 22011The three-move sequence entered the OEIS (A186783)
- 32020The conjecture is recorded: ceiling = n(n−1)/2
- 42025The CayleyPy RL paper: 34 authors, AI, brute force, a competition and bounds
- 52026Journal publication; Antiufeev's preprint: a lower bound of exactly the number of pairs
Proven: n(n−1)/2 − n/2 − 1 ≤ diameter ≤ n(n−1)/2 + 3n; equality and the uniqueness of the mirror are conjectures. The paper has small inconsistencies: the arXiv abstract writes the lower bound without the “− 1”, and section 3.3 refers to table 8 instead of table 6.
Frequently asked questions
A map of all states of a puzzle or group: each state is a point, each allowed move is a line to a neighboring state. For the ring of balls it is all arrangements and the three moves between them.
The largest number of moves the shortest path between two states may need. For the Rubik's Cube it is called God's number: 20 moves if a half turn counts as one move.
By the conjecture, n(n−1)/2 — the number of pairs of balls, from four balls on. Brute force confirms it up to 15 balls; it is proven to lie between n(n−1)/2 − n/2 − 1 and n(n−1)/2 + 3n.
Not fully. In 2026 one of the authors posted a proof of the lower bound of exactly the number of pairs, but it is a preprint; the upper bound is still 3 moves per ball higher.
A compass network learns from random walks to guess the distance to the goal, and beam search keeps many of the best states by its estimate at once so one error doesn't lead into a dead end.
Yes: the authors' code is open and runs for free on Kaggle, and the CayleyPy library is on GitHub (links below).