Egor Urvanov

Which arrangement of balls on a ring takes longest to untangleAI, brute force and the number-of-pairs conjecture

How many moves the most tangled arrangement of balls on a ring with a turn and a swap needs, and what the diameter of a Cayley graph is. How brute force, a neural network with beam search, a Kaggle competition and proofs converged on n(n−1)/2 — the CayleyPy RL paper (arXiv 2502.18663) explained with diagrams.

· 13 min · · Research

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.

frame — the window: only the two balls in it can be swapped · ball color = its number123456in order234561↺ left213456⇄ swap in the window↻ right — the same the other way

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.

AlexanderChervovMarkObozovAlexanderSoibelmanSergeyLytkinIgorKiselevSergeiFironovAndreyLukyanenkoAntoninaDolgorukovaAndreyOgurtsovFedorPetrovStanislavKrymskiiMikhailEvseevLiliaGrunwaldDenisGorodkovGrigoriiAntiufeevGordeyVerbiiVladislavZamkovoyLiudaCheldievaIvanKoltsovArseniySychevAntonEliseevSergeiNikolenkoNursultanNanrynbaevRustemTurtaevNikitaRokotyanSviatoslavKovalevAlexeiRozanovVeniaminNelinSergeiErmilovLidiaShishinaDariyaMamayevaAntoninaKorolkovaKirillKhoruzhiiAlexeyRomanov34 authors · CayleyPy · arXiv, 25 February 2025 · ATMP journal, 2026blue — equal contribution (first two) and the author of the 2026 proof

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.

0113263104165246297218693101floorceiling: 10 movesexactly one sits therein order120 in totalbar width —arrangementson the floor

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.

  1. 1
    Floor
    How many moves from the arrangement to order along the shortest path
  2. 2
    Ceiling
    The top floor: enough moves for any arrangement; God's number, the graph diameter
  3. 3
    Question
    Who 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.

dashed line — the axis through the window: balls swap in pairs across it123456in order216543mirror · floor 15654321reversed · floor 13axis through the window

Left: order; center: the mirror (dashed — the axis through the window); right: reversed. ▶ 1:52

5 balls1208 balls40 32010 balls3.6 M12 balls479 M15 balls1.3 T20 balls2.4·10¹⁸exhaustive search limit — 15 balls, several days of compute

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.

5 balls: 10 pairs = ceiling 106 balls: 15 pairs = ceiling 15n·(n−1)/2each line is one pair of balls; there are as many pairs as moves to the ceiling

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.

BallsPairsCeiling by brute force
466
51010
61515
104545
15105105
Takeaway
In this exampleBrute force up to 15 balls gave both the answer “mirror” and the formula “number of pairs”.
In generalExhaustively solving small cases is a cheap way to get a conjecture you then test far beyond their reach.
Next stepBefore building a clever method, compute the small cases head-on and look for the pattern.

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.

pairs190classic program217,944by the conjecture nothing beats the pairs — the program gives a path a thousand times longer41 minutes · 1,147 times longer · bar broken, not to scale

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.

35142inputarrangementneural networkoften 1 layer · 128 neurons≈ 7outputfloor: moves to order

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.

grey — steps the walk made, blue — the true floor: the walk overestimatesmoves from order03691204812walk stepstep 12floor 6

The walk from the video: grey — the walk's step, blue — the true floor. ▶ 3:42

Input
An arrangement: the list of ball numbers around the ring
Compass
A simple neural network (one layer of 128 neurons is often enough) or gradient boosting
Output
An estimate: how many moves to order

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.

number — balls; blue — path of exactly the pairs, grey — a longer path912162024282932364044= pairsexactly the pairsfound, but longerheight — path / pairs

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

1try three moves from every arrangement in the front
2drop duplicates, the compass scores them
3keep the best and repeat

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.

3416253 and 4 already in order — no swap2040100no hintwith hint100 balls: 9 hours, path 16,632 vs 4,950 pairs

Left: the window where a swap is forbidden; right: how many balls can be untangled. ▶ 4:54

Takeaway
In this exampleOne rule taken from the structure of the problem grew solvable rings from 40 to 100 balls.
In generalProblem knowledge in the form of a ban on clearly useless steps works better than tuning the model.
Next stepLook for moves that never bring you closer to the goal and strike them from the search.

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.

2510204080120160200ring size grows with the number of balls · check — reached exactly the pairsshorter than the pairs — 0 solutions

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.

balls 1, 2, 4 are circled · the line is their triple · the arrow is the direction123456↻ clockwise345612↻ after a turn — the same213456↺ after a swap — flipped

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.

10 balls39754520 balls179250190100 balls489952504950line — the “pairs” conjecture; the more balls, the narrower the fork relative to the ceiling

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.

Brute forceup to 15 balls: mirror and pairs
Compass and frontexact up to 28, 100 with the hint
Competitionup to 200 balls, nothing shorter
Proofa fork around the number of pairs

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 videoIn the paperHow it works
ring with all arrangementsLRX Cayley graph of the symmetric group Sₙvertices are permutations, edges are moves
movesgenerators L, R, XL — left shift, R = L⁻¹, X = (0, 1) — swap of the first two positions
ceilinggraph diameterconjecture: n(n−1)/2 for n ≥ 4
mirrorlongest element ℓₙ(1 0 n−1 … 3 2) = R² · (reversed order)
compassnetwork on diffusion distance + Q-learningfirst learns from walks, then is checked against the Bellman equation d(s) = 1 + min d(t) over neighbors t
frontbeam searchwidth up to 2²⁰; the hint is the X-trick
forkpropositions 4.2 and 4.3n(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 False

A 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.

LinkWhere
directthe TopSpin (Oval Track) and Hungarian Rings puzzles work almost the same way
directprocessor networks: the diameter of the link graph is the longest message delay
directshortest paths in huge permutation graphs where brute force is impossible
analogyplanning: 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

  1. 1
    1968
    Viktor Glushkov: the ring problem with two moves
  2. 2
    2011
    The three-move sequence entered the OEIS (A186783)
  3. 3
    2020
    The conjecture is recorded: ceiling = n(n−1)/2
  4. 4
    2025
    The CayleyPy RL paper: 34 authors, AI, brute force, a competition and bounds
  5. 5
    2026
    Journal 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

What is a Cayley graph?
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.
What are a graph's diameter and God's number?
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.
What is the diameter of the ring with a turn and a swap?
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.
Is the conjecture proven?
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.
How does a neural network find a path in a huge graph?
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.
Can I try it myself?
Yes: the authors' code is open and runs for free on Kaggle, and the CayleyPy library is on GitHub (links below).
Numbers come from A. Chervov, M. Obozov, A. Soibelman, …, K. Khoruzhii, A. Romanov, arXiv 2502.18663 (tables 2–6, propositions 4.2–4.3), and OEIS A186783. Floors for five and six balls were recomputed by brute force; the compass diagram's example sits on floor 7. The blog author co-authored the first CayleyPy paper, on the Rubik's Cube.
  • AI agent
  • research
  • neural networks
  • mathematics
  • Cayley graphs
  • pathfinding
© 2026 Egor Urvanov