\documentclass[11pt,a4paper]{article} \usepackage[T1]{fontenc} \usepackage[utf8]{inputenc} \usepackage{lmodern,microtype} \usepackage[margin=25mm]{geometry} \usepackage{amsmath,amssymb,mathtools} \usepackage{booktabs,longtable,tabularx,array} \usepackage{graphicx,xcolor,tikz} \usetikzlibrary{arrows.meta,positioning,calc,decorations.pathreplacing} \usepackage{enumitem} \usepackage[unicode,colorlinks=true,linkcolor=blue,urlcolor=blue, citecolor=blue]{hyperref} \hypersetup{pdftitle={Exact directed L/X diameter of the symmetric group}} \usepackage{authblk} \setlist{itemsep=2pt,topsep=5pt} \setlength{\emergencystretch}{2em} \setcounter{tocdepth}{2} \newcommand{\res}[2]{\left[#1\right]_{#2}} \newcommand{\ind}[1]{\mathbf{1}_{#1}} \newcommand{\Lop}{\mathrm{L}} \newcommand{\Xop}{\mathrm{X}} \newcommand{\moves}{\text{moves}} \newcommand{\swaps}{\text{swaps}} \newcommand{\shifts}{\text{shifts}} \definecolor{lxblue}{RGB}{32,105,168} \definecolor{lxorange}{RGB}{188,99,27} \tikzset{card/.style={draw=lxblue,rounded corners=2pt,fill=blue!4, minimum width=7mm,minimum height=8mm,font=\small}, point/.style={circle,draw=lxblue,fill=blue!4,inner sep=2pt,font=\small}, arr/.style={-{Stealth[length=2mm]},thick,lxblue}} \title{Exact diameter of the symmetric group with LX generators} % \author{S. Uskov \and A. Chervov \and F. Levkovich-Maslyuk % \and I. Litvak \and A. Smolensky} \renewcommand\Affilfont{\small\itshape\raggedright} \author[1]{S.~Uskov} \author[2]{A.~Chervov} \author[3]{F.~Levkovich-Maslyuk} \author[4]{I.~Litvak} \author[5]{A.~Smolensky} \author[6]{A.~Soibelman} \affil[1]{Independent researcher\\ {\normalfont\nolinkurl{specm32@gmail.com}}} \affil[2]{Institute Imagine, Paris, France\\ {\normalfont\nolinkurl{al.chervov@gmail.com}}} \affil[3]{Centre for Mathematical Science, City St George's, University of London, Northampton Square, EC1V 0HB, London, UK\\ {\normalfont\nolinkurl{fedor.levkovich@gmail.com}}} \affil[4]{Independent researcher\\ {\normalfont\nolinkurl{Vanya.litvak@gmail.com}}} \affil[5]{Neapolis University Pafos, Cyprus\\ {\normalfont\nolinkurl{andrei.smolensky@gmail.com}}} \affil[6]{IHES, France\\ {\normalfont\nolinkurl{asoibelman@gmail.com}}} % \affil[1]{Independent researcher} % \affil[2]{Institute Imagine, Paris, France} % \affil[3]{Centre for Mathematical Science, City St George’s, University of London, Northampton Square, EC1V % 0HB, London, UK} % \affil[4]{Independent researcher} % \affil[5]{Neapolis University Pafos, Cyprus} % \affil[6]{IHES, France} \date{} \begin{document} \maketitle \begin{abstract} We prove an exact formula for the diameter of the directed Cayley graph for the symmetric group $S_n$ with generators $L$ (left cyclic shift) and $X$ (exchange of the first two elements). For every $n \geq 5$, we show that the diameter equals $(3n^2-8n+12)/4$ for even $n$ and $(3n^2-8n+9)/4$ for odd $n$. This resolves a longstanding question first proposed in the 1960's and refined in more recent work. The proof is AI-assisted and is based on a variety of combinatorial arguments. We also describe the agentic system we developed that led to this proof and looks highly promising for attacking other questions of this type. An accompanying Lean 4 formalization rigorously verifies the result. \end{abstract} \tableofcontents \bigskip % Place cards bearing distinct numbers in a row. Two operations are allowed: % move the first card to the end, or exchange the first two cards. How many % moves are needed to put the cards in increasing order? % The answer depends on the starting row. We seek the largest possible length % of a shortest solution. \newpage \section{Introduction} In this paper we study the following problem. Consider a permutation of the integers $1,2,\dots,n$, and consider two operations: $L$, a left cyclic shift, and $X$, exchange of the first two elements. How many operations does it take to sort the sequence, in the worst case? Despite its elementary formulation, this combinatorial problem is far from trivial. In group theory terms, it provides the diameter of the directed Cayley graph of the permutation group $S_n$ when generated by elements $L$ and $X$ (notice the graph is directed here because we do not include the inverse $L^{-1}$ generator). This question was first proposed in the 1960's in \cite{glushkov1968}, while a precise conjecture for the result was stated recently in \cite{cayleypygrowth} (a more detailed review of the literature is given in the next section). Here we prove this conjecture and thus resolve this longstanding open question. Ultimately, one of our motivations is attacking the celebrated Babai-type conjecture of establishing a polynomial bound on the $S_n$ diameter with \textit{arbitrary} generators \cite{BabaiSeress1988,BabaiSeress1992,BabaiBealsSeress2004,BreuillardGreenTao2011,HelfgottSeress2014} which is however far beyond the scope of the present work.\footnote{We also hope the results of this work and proofs for other related settings may help advance the `holographic'-like duality conjectured in \cite{CayleyPyHolography2026}.} Initially we attempted to attack the problem by usual LLM tools -- direct conversations with LLMs such as GPT Astra or Claude Fable 5.1/Opus 5.5. In our experiments we found that this approach does not lead to success (or perhaps would require a huge token budget). This highlights the difficulty of this problem. For example, the models typically get stuck at pursuing microscopic improvements of known upper bounds for the desired quantity (diameter) rather than developing new ideas. Remarkably, we were able to circumvent this obstacle and indeed obtain a proof -- by developing an LLM-powered agentic system. We describe it in more detail in the next subsection. We also provide a Lean formalization of the result, see section \ref{sec:lean}. While currently we have not understood the proof in detail, we hope that, together with a description of our workflow, it will still be of use to the community. Our agentic system has shown itself to be quite powerful while staying at reasonable amounts of token spend and compute. It is this system that obtained the proof we present (with relatively minor human intervention that mainly amounted to agent orchestration rather than mathematical input). We already have evidence of its high efficiency in obtaining proofs of other conjectures of this type. We hope to explore its highly promising full capabilities in the future. \subsection{Agentic system} To obtain the proof presented in this paper we used an agentic system developed primarily by one of the authors (S. Uskov) and available at the public GitHub repository (\href{https://github.com/snow-ghost/research-team/}{link}). It establishes a `Research Team' consisting of several agents powered by an LLM (we mainly used GPT Astra in this work). The system provides skills and instructions including reasoning frameworks that encourage the agents to use a range of reasoning strategies and tricks -- such as looking for counterexamples, trying to reformulate the problem in different ways, etc. Overall it is a prototype platform for coordinating AI-assisted research in mathematics, under human supervision. It combines a web interface, a Go server, and a persistent database to organize conjectures, lemmas, dependencies, research tasks, and attempts at problems. Configurable agents --including Codex, Claude Code, OpenCode, and Coddy Agent -- take on distinct tasks such as finding proofs, searching for counterexamples, formalizing arguments in Lean, and reviewing results. A planner proposes overall research strategies, while execution and repair cycles advance approved tasks. Lean code is used to check candidates against a fixed formal statement, and the human operator provides final acceptance of the results in some cases. The system also records versioned evidence, reviews, failed approaches, and reusable accepted lemmas, allowing research results to accumulate across attempts (and, when enabled, across projects). Full technical details can be found at the repository linked to above. \subsection{AI statement} Section 1 of the paper is mostly human-written, while the subsequent text and figures, including the proof, are AI-generated with minor human edits. The proof was obtained by our agentic system with relatively minor human intervention that amounted mostly to workflow orchestration. We mainly used GPT-6 Astra (with reasoning level from Medium to Ultra) within the agentic system as well as within OpenAI Codex. We also used Opus 5.5 for minor checks and editing. \section{Related work} \paragraph{The original problem and earlier bounds.} The two-generator LX problem goes back to Glushkov's work on `completeness of operation systems' in computers~\cite{glushkov1968}. The historical account by Glukhov and Zubov~\cite[pp.~20--21]{glukhovzubov1999} records Glushkov's upper bound $2n^2$, Pogorelov's subsequent quadratic bounds in 1973~\cite{pogorelov1973}, and Zubov's asymptotic result $D(n)\sim3n^2/4$, attributed there to 1979. Zubov's later paper treats diameter bounds for a complete cycle and a transposition~\cite{zubov1998}. His subsequent work studies representations using generalized inversions and sorting using circular inversions~\cite{zubov2010,zubov2016}. These early results do not specify the parity-dependent constant terms in the exact formula proved here. \paragraph{The precise conjecture and computations.} The exact formula for the diameters was conjectured very recently by Chervov and collaborators in their CayleyPy growth paper~\cite{cayleypygrowth}. They report exhaustive diameter computations through $n=15$. Thus the attribution distinguishes Glushkov's original problem from this later precise conjecture. The directed diameter sequence is recorded as OEIS A039745~\cite{oeisdiameter}. Moreover, OEIS A378881 records computational examples of permutations at maximum directed distance~\cite{oeisextremal}. The present argument proves the conjectured formula uniformly for all $n\ge5$. \paragraph{Related questions.} The CayleyPy reinforcement-learning study~\cite{cayleypyrl} supplies pathfinding methods and diameter bounds chiefly for the three-generator system $L,R,X$, where $R=L^{-1}$ also costs one move. Its bounds therefore concern a different metric from the directed $L,X$ metric of this paper. Kuppili and Chitturi~\cite{kuppilichitturi2020} use the same two moves, calling them $L,E$, but study sorting the reversed permutation rather than the maximum distance over all permutations. The same directed graph is also called the sigma--tau graph. Williams' preprint~\cite{williams2013} and the later work of Sawada and Williams~\cite{sawadawilliams2020} concern Hamilton paths and cycles in that graph. Their objective is to visit every permutation, rather than to determine the longest shortest path. Adin, Alon, and Roichman~\cite{adinalonroichman2025} determine the adjacent-swap diameter for circular permutations, where cyclic rotations identify arrangements. That problem does not charge shifts or specify a final starting marker, whereas both are essential to the constants in the present formula. The proof below includes its combinatorial estimates explicitly. For background on comparing ordinary inversion count with total absolute displacement, see Diaconis and Graham~\cite{diaconisgraham1977}. \bigskip Note added: we learned that after our proof was completed A. Guerrieri also independently obtained \cite{GuerrieriInPreparation} the proof of several related conjectures from \cite{cayleypygrowth} (though not specifically the one we prove here). \section{The moves}\label{sec:moves} We will describe the setting of the problem in the following way. We start with cards labelled $0,1,\ldots,n-1$, where $n$ is the number of cards. All labels are distinct. We have the following L and X moves: \textbf{Shift $\Lop$:} Left cyclic shift. Equivalently, we remove the first card and place it at the end. \textbf{Swap $\Xop$:} exchange the first and second cards. Figure~\ref{fig:operations} shows both operations applied to the same row. \begin{figure}[htbp] \centering \begin{tikzpicture}[x=9mm,y=11mm] \foreach \v [count=\i from 0] in {2,0,4,1,3} \node[card] at (\i,2) {\v}; \node[anchor=east] at (-.6,2) {Starting row}; \foreach \v [count=\i from 0] in {0,4,1,3,2} \node[card] at (\i,1) {\v}; \node[anchor=east] at (-.6,1) {After $\Lop$}; \foreach \v [count=\i from 0] in {0,2,4,1,3} \node[card] at (\i,0) {\v}; \node[anchor=east] at (-.6,0) {After $\Xop$}; \end{tikzpicture} \caption{Two operations on five cards. The shift moves card 2 to the end; the swap exchanges cards 2 and 0.}\label{fig:operations} \end{figure} The target row is $(0,1,2,\ldots,n-1)$. A backward shift is not a single permitted move: moving the last card to the front using $\Lop$ requires $n-1$ shifts. For each starting row, take the length of a shortest solution. The maximum of these lengths is the \textbf{diameter}, denoted by $D(n)$. For example, $D(5)=11$ asserts both that every row of five cards can be sorted in at most 11 moves and that some row cannot be sorted in 10 moves. The same diameter governs transitions between arbitrary pairs of rows. Given a target row, rename its first card 0, its second card 1, and so on. The target becomes the increasing row. Renaming cards changes neither operation and therefore preserves the length of every shortest transition. \section{The exact formula}\label{sec:formula} For $n\ge5$, the result is \[ D(n)= \begin{cases} \frac34 n^2-2n+3,&n\text{ even},\\[4pt] \frac34 n^2-2n+\frac94,&n\text{ odd}. \end{cases} \] Write $B$ for the appropriate right-hand side. To prove $D(n)=B$, we prove that every row has a solution of length at most $B$, and that some row has no solution shorter than $B$. These are the upper and lower bounds. For six and seven cards, respectively, \[ D(6)=\frac34\cdot36-12+3=27-12+3=18, \qquad D(7)=\frac34\cdot49-14+\frac94=25. \] \begin{center} \begin{tabular}{lrrrrrr}\toprule $n$&5&6&7&8&9&10\\ $D(n)$&11&18&25&35&45&58\\\bottomrule \end{tabular} \end{center} This is the formula of~\cite[Conjecture~11]{cayleypygrowth}. The formulas for the even and odd cases differ only in their constant terms. A function given by different polynomials for different residue classes is called a \textbf{quasipolynomial}; here the residue class is the parity of $n$. An explicit route proves only an upper bound for its starting row. Proving that a route is shortest requires excluding every shorter solution. \section{Eleven moves on five cards}\label{sec:five} Consider the row $(1,0,3,4,2)$. \begin{center} \begin{tabular}{rcl}\toprule Step&Operation&Row after the operation\\\midrule 0&--&$(1,0,3,4,2)$\\ 1&$\Lop$&$(0,3,4,2,1)$\\ 2&$\Lop$&$(3,4,2,1,0)$\\ 3&$\Lop$&$(4,2,1,0,3)$\\ 4&$\Lop$&$(2,1,0,3,4)$\\ 5&$\Xop$&$(1,2,0,3,4)$\\ 6&$\Lop$&$(2,0,3,4,1)$\\ 7&$\Lop$&$(0,3,4,1,2)$\\ 8&$\Xop$&$(3,0,4,1,2)$\\ 9&$\Lop$&$(0,4,1,2,3)$\\ 10&$\Xop$&$(4,0,1,2,3)$\\ 11&$\Lop$&$(0,1,2,3,4)$\\\bottomrule \end{tabular} \end{center} There are eight shifts and three swaps. After step 10, the circular order is already correct. The last shift makes the row begin with 0, and it must be included in the exact move count. For five cards, all minimum distances can be checked by finite enumeration. Start at the increasing row. At each stage, construct predecessors of rows already found by applying $\Xop$ or moving the last card to the front. The backward shift is used to find a predecessor; it is not an allowed forward move in a solution. Record each new row only when it first appears. The stage at which a row first appears is its minimum distance to the target using the permitted operations. The resulting counts are \begin{center} \begin{tabular}{rrrr}\toprule Distance&Rows&Distance&Rows\\\midrule 0&1&6&15\\ 1&2&7&20\\ 2&3&8&22\\ 3&5&9&19\\ 4&8&10&11\\ 5&12&11&2\\\bottomrule \end{tabular} \end{center} The counts sum to $120=5\cdot4\cdot3\cdot2\cdot1$, so they cover every permutation of five cards. The example first appears at stage 11. Its solution is therefore shortest, and $D(5)=11$. This enumeration applies only to five cards. \section{Cards on a circle and the starting marker}\label{sec:circle} Arrange the positions on a circle and label them $0,\ldots,n-1$. Add a marker indicating where the row currently starts. Under $\Lop$, the cards remain in their circular positions and the marker advances by one position. Under $\Xop$, the cards at the marker and at the next position are exchanged. After $n$ shifts, the marker returns to its starting position. To distinguish full turns, repeat the circle along an infinite line. After positions $0,\ldots,n-1$, put positions $n,\ldots,2n-1$, then another copy, and continue. The line also extends to negative coordinates. \begin{figure}[htbp] \centering \begin{tikzpicture}[x=7mm,y=9mm] \draw[arr] (-.5,0)--(12.8,0); \foreach \i in {0,...,12}{ \pgfmathtruncatemacro{\r}{mod(\i,5)} \node[point] at (\i,0) {\r}; \node[font=\scriptsize,below=4mm] at (\i,0) {\i};} \draw[arr,lxorange] (2,.4) to[bend left=22] node[above,font=\small] {$+5$} (7,.4); \draw[arr,lxorange] (7,.4) to[bend left=22] node[above,font=\small] {$+5$} (12,.4); \node[font=\small,anchor=west] at (0,-1.5) {Inside circles: circular positions; below: integer coordinates.}; \end{tikzpicture} \caption{Unrolling a circle with five positions. Coordinates 2, 7, and 12 represent the same circular position on different turns.}\label{fig:unroll} \end{figure} The displacement from coordinate 2 to coordinate 7 is $+5$, whereas that from 2 to 12 is $+10$. Both return to circular position 2, but they use different numbers of turns. Integer coordinates must therefore be retained when estimating the number of moves. \section{Move conventions and position labels}\label{sec:displacements} We distinguish three quantities: \begin{itemize} \item the \textbf{label} written on a card; \item the card's \textbf{initial position}, before any move; \item the card's \textbf{target coordinate} on the unrolled line. \end{itemize} Let $p(i)$ be the label of the card initially at position $i$. The symbol $p$ denotes the entire starting row: $p(2)=4$ means that position 2 initially contains card 4. Let $T_i$ be the target coordinate of the card initially at position $i$. The index $i$ always refers to that initial position. The displacement of this card is \[ u_i=T_i-i. \] A positive displacement is to the right and a negative displacement is to the left. Displacement is final coordinate minus initial coordinate. A card that moves back and forth can travel farther than the magnitude of its displacement. \begin{figure}[htbp] \centering \begin{tikzpicture}[x=14mm,y=10mm] \draw[arr] (-.3,0)--(5,0); \foreach \i in {0,...,4}{\draw (\i,.08)--(\i,-.08);\node[below] at (\i,-.08) {\i};} \node[card] (a) at (2,.6) {4}; \node[card,dashed] (b) at (4,.6) {4}; \draw[arr,lxorange] (2,1.15)--node[above] {$u_2=4-2=+2$}(4,1.15); \node[above,font=\small] at (2,1.95) {Initial position}; \node[above,font=\small] at (4,1.95) {Target coordinate}; \end{tikzpicture} \caption{Card 4 starts at position 2. Assigning target coordinate 4 gives displacement $+2$. The card's label and its initial position differ.}\label{fig:displacement} \end{figure} For example, the row $(2,0,4,1,3)$ permits targets without additional turns: \begin{center} \begin{tabular}{lrrrrr}\toprule Initial position $i$&0&1&2&3&4\\ Card label $p(i)$&2&0&4&1&3\\ Target $T_i$&2&0&4&1&3\\ Displacement $u_i$&$+2$&$-1$&$+2$&$-2$&$-1$\\\bottomrule \end{tabular} \end{center} The displacements sum to zero. A swap moves one card one position right and the other one position left. In the circular model, $\Lop$ moves only the marker and leaves the card coordinates unchanged. More generally, the marker may finish at circular position $c$. Card $p(i)$ must then occupy position $p(i)+c$ modulo $n$. The unrolled targets satisfy \[ T_i\equiv p(i)+c\pmod n, \qquad \sum_i(T_i-i)=0. \] The first condition specifies the target modulo a full turn; the second specifies zero total displacement. Residues alone do not determine the move count, since different numbers of turns can require different routes. \section{Constructing a sorting scan}\label{sec:scan} Fix target coordinates. At the marker, compare the targets of the two adjacent cards. If the left target exceeds the right target, apply $\Xop$. Otherwise apply $\Lop$ and advance to the next comparison. The same operations take place in every periodic copy. An \textbf{inversion} is a pair in the wrong target order. On a finite line, the left card should end to the right of the right card. On the periodic line, pairs from different copies must also be counted. To obtain a finite count, take initial positions $i0$ becomes backward. The arrow ending at 0, if it starts at a positive position, ceases to be backward when its end wraps to $n-1$. If 0 is at position 0, both changes are absent: $0\to0$ does not become backward, and no backward arrow ending at 0 disappears. In either case, \[ H(x+1)=H(x)+f(x)-1. \] The index is cyclic. Since $f(x)\ge0$, the decrease is at most one. Two consequences will be used: \begin{enumerate} \item An absent offset has $f(x)=0$, so the next height is one lower. \item A minimum height occurs at an offset that is present; otherwise the next height would be lower than the minimum. \end{enumerate} The same rule can be summed over consecutive columns. If $k$ is subtracted modulo $n$ from every entry of a table $q$, let $N_k$ count its relative offsets $\res{q(i)-i}{n}$ that are less than $k$. Then \[ \text{new height}=\text{old height}+N_k-k. \] In particular, equal heights imply $N_k=k$. For $q_2=(0,5,2,6,4,1,3)$, the relative offsets are $(0,4,0,3,0,3,4)$. Three are below 2, so subtracting another 2 changes the height from 2 to $2+3-2=3$, taking $H(2)$ to $H(4)$. For an offset $x$, its \textbf{backward gap} is $g=\res{-x}{n}$. This is $n-x$ for $x>0$ and 0 for $x=0$. With $n=7$, offset 5 has gap 2. \subsection{From table height to a swap bound} Fix a table $q$ of height $H$. At position $i$, first choose the forward target $q(i)$ if $q(i)\ge i$, and $q(i)+n$ otherwise. Then decrease every target by $H$. The added turns sum to $nH$, and decreasing $n$ targets by $H$ subtracts $nH$. The resulting displacements are balanced and lie between $-H$ and $n-H-1$. We prove the following bound on the inversion, hence swap, count: \[ \boxed{I\le H(n-H-1).} \] Let $V$ be the ordinary inversion count of $q$, and let $A$ be the sum of backward-arrow lengths $i-q(i)$ over $q(i)i>q(i)$ and contributes no negative term. The remaining terms are exactly the between-class inversions. This proves the identity for $I$. Next we show $V\le2A-H$. Temporarily allow swaps of arbitrary positions in the finite row, solely to count $V$. These are not additional puzzle moves. Until the row is increasing, find $ii$ and follow the values starting at $q(i)$. To the right of $i$, arrows cannot go forward. A fixed position cannot occur along this path, since it would already receive an arrow from a distinct position, contradicting injectivity. The path decreases until it reaches a number at most $i$; its last position before that step can serve as $j$. Swapping positions $i,j$ brings both cards toward their labelled positions and decreases $A$ by exactly $j-i$. Repeat until the row is increasing. If there are $k$ swaps, their distances sum to the original $A$. A swap at distance $r$ can be implemented with $2r-1$ adjacent swaps, by carrying one card across and restoring the intermediate cards. Thus the construction uses $2A-k$ adjacent swaps. Since their minimum number is $V$, we have $V\le2A-k$. Each chosen swap decreases the number of backward positions by at most one. That number starts at $H$ and ends at zero, so $k\ge H$. Hence $V\le2A-H$, and substitution gives \[ I\le H(n-H)+(2A-H)-2A=H(n-H-1). \] Define the nonnegative \textbf{reserve} \[ S=H(n-H-1)-I\ge0. \] A scan with $\Lambda$ shifts has total length $H(n-H-1)+\Lambda-S$. \subsection{What a card's backward-swap count means} For the card initially at $i$, count the cards to its left on the unrolled line whose targets exceed $T_i$, including copies from preceding turns. Call them its \textbf{backward predecessors}, and write their number as $b_i$. During the scan, the card must let each predecessor pass by moving one position left. Thus $b_i$ counts its necessary backward swaps and is also called its \textbf{depth}. Depth differs from displacement, since the card may also take part in rightward swaps. A displacement table is \textbf{balanced} if its entries sum to zero. For a balanced table with $u_i\ge-H$ for every $i$, all depths satisfy $b_i\le H$. This can be proved without analysing a complete route. Choose a card of maximum depth. If it has a forward crossing with another card, the other card has strictly greater depth: every larger target that the first card must let pass also exceeds the smaller target of the other card, and the first card itself supplies an additional predecessor. This contradicts maximality. The chosen card therefore has no forward crossings. Its displacement is minus its depth, and the lower bound on displacement bounds that depth by $H$. All other depths are no larger. For the targets constructed from a table of height $H$, the lower displacement bound is exactly $-H$. Depth $H$ occurs precisely at the fixed positions of $q$. A card of depth $H$ has no forward crossings and therefore displacement $-H$, which in this construction corresponds to a fixed position. Conversely, displacement $-H$ requires at least $H$ backward swaps, and more than $H$ are impossible. A fixed position here means $q(i)=i$ in the auxiliary table. The card starting there is assigned displacement $-H$ during the scan. The completion estimates distinguish depths $H$, $H-1$, and smaller depths. Encountering the third kind early saves a turn: in Estimate 2 below, $H$ is replaced by $H-1$. \subsection{How many shifts the swap queue requires} After the first sweep, the front card has the greatest target among those seen. It has no backward predecessors, so its depth is zero. The other cards form a queue of $n-1$ positions. Suppose the next card has depth $b$. If $b>0$, a swap decreases that depth by one and moves the zero-depth card one position forward. After $\Lop$, the front depth is zero again and the processed card goes to the back of the queue. If $b=0$, no swap is needed. Each shift processes one queue position. A card of depth $b$ visits the front of the queue $b$ times, with $n-1$ shifts between visits. If its initial position after the zero-depth front is $i$, its last swap occurs no later than shift \[ i-1+(n-1)(b-1). \] The maximum of these quantities over positive depths bounds completion of the whole queue. During the first sweep, a card moved left by a comparison loses one unit of depth. If the next target is a new maximum, the old maximum enters the queue with depth equal to the least depth in the prefix already seen. The same comparison count will be used for the initial delay in Section~\ref{sec:lower-shifts}. We use two consequences. \textbf{Estimate 1.} If the front depth is less than $H$ and all depths are at most $H$, the scan finishes by shift $H(n-1)-1$. After the first sweep, every queue depth is at most $H-1$; substitute that bound in the last-swap formula. \textbf{Estimate 2.} Suppose the front depth is less than $H$, all positions of depth $H$ are at most $a$, and some position between 0 and $k$ has depth less than $H-1$. Then completion occurs by shift \[ (H-1)(n-1)+\max(a,k)-1. \] After the first sweep, depth $H-1$ can remain only on a former depth-$H$ card or on the card that carried the maximum until the first depth below $H-1$ appeared. The former depth-$H$ cards started at positions at most $a$; the card that carried the maximum started at a position at most $k$. Every other depth is at most $H-2$, so the queue formula gives the estimate. The last swap may precede the required final marker. To finish at residue $c$, append the necessary shifts. The completion times below are always rounded up to a number with the required residue. \subsection{An arrow of almost maximum depth} Let $q$ have height $H$, and let a card at $i$ have depth $H-1$ and relative offset $r$. Its displacement is $r-H$. Displacement equals rightward swaps minus leftward swaps, so its number of rightward swaps is \[ r-H+(H-1)=r-1. \] At each such swap, a smaller-target card moves left. That card has greater depth, hence depth $H$, and its initial position is fixed in $q$. All these fixed positions lie on the open circular arc from $i$ to $q(i)$. That arc has $r-1$ positions, exactly the number of rightward swaps. A pair contributes at most one such crossing because all displacements lie in a band of width less than $n$. Therefore \textbf{every position passed over by an arrow of depth $H-1$ is fixed}. Suppose every depth in an initial segment is at least $H-1$, with $q(0)\ne0$ and $H\ge2$. Every nonfixed arrow in that segment goes forward. Indeed, a backward arrow with nonzero endpoint would pass through position 0 along its circular arc and require $q(0)=0$. A backward arrow ending at 0 would require all subsequent positions to be fixed. Any earlier backward arrow would then also have to end at 0, contradicting distinct endpoints. Only one backward arrow would remain, contrary to $H\ge2$. \subsection{How many initial positions can be fixed} Let $U$ count unit backward arrows $i\to i-1$ in $q$. Retain the notation $A$ for total backward length and $V$ for ordinary inversions. Add one modulo $n$ to every entry, and let $j$ be the original position of label $n-1$. Direct counting gives \[ A'=A+j-H,\qquad V'=V+2j-(n-1),\qquad H'\ge H-U. \] For $A$, each length changes by one and the single label $n-1$ wraps. For $V$, that label loses $n-1-j$ inversions and gains $j$. A backward arrow can cease to be backward only if its length was one, which gives the last inequality. Use $V'+H'\le2A'$, proved above. Substitution cancels $j$ and yields \[ 3H\le n-1+2A-V+U. \] At a minimum height, $U\le1$. The neighbouring-height formula shows that the frequency of the preceding offset is at most one; every unit backward arrow has that offset. If the first $r$ positions are fixed, remove them and renumber the others from zero. The quantities $H,A,V,U$ are unchanged, and the row length becomes $n-r$. Since $S=2A-V-H$, we obtain \[ \boxed{r\le n-2H+S.} \] There is a related bound for a prefix of almost maximum depth. If its first $r$ depths are at least $H-1$ and $q(0)\ne0$, then (assuming also $H\geq 2$) \[ \boxed{r\le n-2H+S+1.} \] To prove it, follow arrows starting at 0. Each goes forward and passes over fixed positions only. Replace consecutive links $0\to b\to q(b)$ by $0\to q(b)$ and make $b$ fixed. The quantities $A,H,U$ are unchanged, while $V$ increases by one: changes involving intermediate fixed positions cancel, leaving the pair of link endpoints. Thus $2A-V$ does not increase. Repeat until positions $1,\ldots,r-1$ are fixed. Move the origin from 0 to 1, changing position numbers and labels together. The quantities $H$ and $2A-V$ are unchanged. The number of unit backward arrows cannot increase except possibly through an arrow from 0 to $n-1$. Such an arrow would pass over all intermediate positions and, together with the backward arrow ending at 0, give height at most one. The hypothesis $H\ge2$ excludes it. There are now $r-1$ fixed initial positions, so the preceding count gives the second bound. In particular, a depth below $H-1$ occurs by position $n-2H+S+1$. \subsection{Which minimum heights remain} In this subsection $H$ is the minimum height, so $H\le(n-1)/2$. An offset attaining that height will be called a \textbf{minimum offset}: its height is minimum, not necessarily its numerical value. If $H>0$, the first backward position is at most $n-H$, since $H$ backward positions must fit in the remaining row. A backward position has depth below $H$. Move the marker there, run the scan, and finish at the required start. Estimate 1 and the swap bound give at most \[ H(n-H-1)+(H+2)(n-1)-H \] moves. If $n=2H+5+s$ with $s\ge0$, the difference between $\frac34 n^2-2n$ and this bound is \[ Hs+\frac{3s^2+14s+3}{4}>0. \] Thus $n\ge2H+5$ already admits a route within the required bound. If $H=0$, the auxiliary table is increasing and at most $n-1$ shifts suffice. For even $n$, we must also exclude $n=2H+4$. Let $g$ be the gap of the chosen offset. Allowing one extra turn, the scan finishes at shift $H(n-1)+n+g$, giving the total bound \[ \frac34 n^2-2n+g-S. \] A counterexample therefore requires $g\ge S+4$. If some position $i\le g+1$ has depth below $H$, start there and finish at $H(n-1)+g$ shifts; the length is then less than $B$. Consequently positions $0,\ldots,g$ must all be fixed. The fixed-prefix bound gives $g+1\le n-2H+S=4+S$, or $g\le S+3$, a contradiction. For even $n=2m$, only $H=m-1$ remains. For odd $n=2m+1$, the possibilities are $H=m-1$ and $H=m$. In the latter case the minimum equals the mean, so all heights are equal. We handle it next. \subsection{Odd size with all heights equal} Let $n=2m+1$, $m\ge3$, and suppose every height equals $m$. The neighbouring-height rule gives $f(x)=1$ for every $x$: all offsets are distinct. Each auxiliary table has one fixed position, $m$ backward arrows, and $m$ forward arrows. There is a forward arrow starting at $a>0$ with length at least $m-2$ and gap at least 3. Otherwise the $m$ distinct forward lengths could only come from the following possibilities: \begin{itemize} \item lengths $1,\ldots,m-3$, at most $m-3$ choices; \item length $n-2$, at most one choice; \item an arrow starting at 0, at most one choice. \end{itemize} Length $n-1$ can occur only from 0 and is already included. This gives at most $m-1$ choices, a contradiction. The chosen arrow has $3\le g\le m+3$. In its table, $q(0)\ne0$. An arrow of depth $m-1$ passes over fixed positions only. As there is just one fixed position, its offset is at most 2. Offsets 0, 1, 2 each occur once, so at most three cards have depth at least $m-1$. Among the first $g+1\ge4$ positions there is a depth below $m-1$. Estimate 2 gives at most \[ m^2-S+2m(m-1)+g-1\le3m^2-m+2-S \] moves. If $S\ge1$, the required bound follows. It remains to treat $S=0$. We show that zero reserve makes the starting row a circular reflection. The equality $S=0$ means $V=2A-m$. The arbitrary-swap construction above gave $V\le2A-k$ and $k\ge m$, so $k=m$. Decompose the arrows of $q$ into cycles. Swapping two positions either joins two cycles or splits one cycle in two, changing the number of cycles by one. Sorting therefore takes at least the number of positions minus the number of cycles. Every nontrivial cycle has at least one forward arrow, so the number of backward arrows is no larger than that same difference. There is one fixed position and $2m$ nonfixed positions. The equality $k=m$ leaves exactly $m$ nontrivial cycles. Each contains at least two positions, so all are transpositions. Draw these pairs as arcs above the row. For $a0$ and $g(0)=0$. For an arrow $i\to p(i)$ this is the forward circular distance from its endpoint $p(i)$ back to its start $i$. The scan restrictions can be collected as follows. \begin{quote} \textbf{Odd-counterexample lemma.} A counterexample satisfies all of the following conditions. \begin{enumerate}[label=\textbf{\Alph*.},leftmargin=*] \item \textbf{Three heights.} We have $n=2h+3$, $h\ge2$, and every height is $h$, $h+1$, or $h+2$. \item \textbf{The initial card.} We have $p(0)=n-d$ with $d=1$ or 2, and $H(p(0))=h+1$. Every present offset of height $h+1$ with positive gap has gap at least $d$. \item \textbf{Minimum height.} If $H(x)=h$, then $g(x)>h+3$. At the last occurrence $i$ of $x$, the original arrow satisfies $00$. Subtracting $t$ from the entries of $q$ gives the table of gap $V$ with the same height, so exactly $t$ relative offsets of $q$ are below $t$. For each $i\in\{V+1,\ldots,G\}$, its arrow cannot end after $G+1$; otherwise position $G+1$ would have to be fixed. Its offset is therefore at most \[ G+1-i\le G-V=t. \] Equality is impossible, since it would make $i$ fixed in the gap-$V$ table, whose last fixed position is at most $V$. All $t$ positions $V+1,\ldots,G$ therefore have offsets below $t$, and they exhaust all such offsets. The offset at 0 is at least $t$ and cannot equal $t$, since the other table also has a nonzero first entry. The first arrow ends after $t$, hence at least at 2, making position 1 fixed. Its offset is zero, below $t$, but $1\le V$ excludes it from the $t$ positions already counted. This contradiction proves uniqueness. \subsection{A preliminary window count without a height bound} For now, heights may exceed $h+2$. Call a column high if its height is at least $h+2$. Let $P$ count high columns, $Z$ count minimum columns, and $E$ be the sum of the excesses above $h+2$. The total-height identity gives \[ Z=P+E. \] The minimum-offset count from Section~\ref{sec:even-upper} gives $Z\le h+1$. That count used minimum-height scan bounds and did not depend on parity. Let $W$ be the window of offsets with gaps from 1 to $h+3$; it has $h+3$ elements. A minimum offset in this window can only be the initial offset $p_0=p(0)$, since all other short minimum gaps have been excluded. If $p_0$ is minimum and belongs to the window, let its gap be $d$ and its frequency be $F$. If $F\le d+1$, a depth below $h$ would appear by $d+1$ and a scan would fit within $B$. Thus $F\ge d+2$. On passing from $p_0$ to offset 0, at least $F$ zero relative offsets are counted and $d$ is subtracted, so $H(0)\ge h+2$. Thus the minimum-offset set can meet the window only in $p_0$. At the same time, the high column at offset 0 lies outside their union. The overlap on the left of a count can be cancelled against this extra high column on the right. An offset in the window falls into one of the following categories: \begin{enumerate} \item height at least $h+2$; \item the initial offset $p_0$; \item the preceding offset $p_0-1$ modulo $n$, when the initial height is minimum; \item the unique exceptional offset $z$ of height $h+1$, if it exists; \item a backward arrow with nonzero endpoint and height $h+1$. \end{enumerate} To check completeness, a minimum height must be $p_0$. For a present offset of height $h+1$, take its last occurrence. It is initial, exceptional if the arrow goes forward or ends at 0, or in the last category. An absent offset has next height one lower, hence minimum. The next gap lies between 0 and $h+2$, so the next offset is $p_0$ and the absent one is $p_0-1$. Define three events: \begin{itemize} \item $T$: the initial height is minimum, or is $h+1$ with positive initial gap in the window; \item $K$: the initial height is minimum and $p_0-1$ belongs to the window; \item $Q$: an exceptional offset exists, and another offset of the same height $h+1$ has a smaller positive gap. \end{itemize} An event has indicator 1 when it holds and 0 otherwise. We claim \[ \boxed{E+2\le\ind{T}+\ind{K}+\ind{Q}.} \] First suppose a present height-$(h+1)$ offset lies in the window. Choose one with smallest positive gap and call its offset $t$. Every other window offset of that height is numerically less than $t$. Noninitial minimum offsets are also less than $t$, since their gaps exceed the entire window. Select backward arrows whose offsets are below $t$. They remain backward after subtracting $t$. If $t\ne p_0$, the new table also has a backward arrow ending at 0. Hence at most $h+1-\delta$ arrows were selected, where $\delta=1$ if $t\ne p_0$ and $\delta=0$ otherwise. Count $t$ separately. Together with the initial offset, it requires at most $\ind{T}+\delta$ places; a coincident $t=p_0$ needs no second place. The preceding offset requires at most $\ind{K}$ places. The exceptional offset needs an additional place only if its gap exceeds the smallest chosen gap, which is exactly event $Q$; at equality it is already $t$. Including the overlap cancellation gives \[ Z+h+3\le P+(h+1-\delta) +(\ind{T}+\delta)+\ind{K}+\ind{Q}. \] Use $Z-P=E$ to obtain the claim. If no present height-$(h+1)$ offset lies in the window, high columns and the initial and preceding offsets cover the window. Then $h+3\le P+\ind{T}+\ind{K}$. Since $P=Z-E\le h+1-E$, the same claim follows, with $Q$ absent. \subsection{Excluding heights above \texorpdfstring{$h+2$}{h+2}} The preliminary bound gives $E\le1$. If $E>0$, then $E=1$ and all three events $T,K,Q$ hold. In particular, the initial height is minimum and its gap $d$ lies between 0 and $h+3$. Gap 0 means $p_0=0$. We show that under these conditions $E=0$. Take threshold $t=p_0-1$ modulo $n$. If $d>0$, the absence of a short scan gives $F\ge d+2$, where $F$ is the frequency of $p_0$. Since every height is at most $h+3$, the next-height formula gives $F\le4$, hence $d\le2$. The height at $t$ is at most $h+1$, since the next column is the initial minimum $h$. It cannot be $h$: it is noninitial and has gap $d+1\le3$, whereas noninitial minimum gaps exceed $h+3$. Thus $H(t)=h+1$, and $t$ is absent. Every present height-$(h+1)$ offset is strictly below $t$. Indeed, between $p_0$ and the end of the scale there are fewer than $d$ offsets. Passing to any of them counts at least $F$ offsets and subtracts fewer than $d$, giving height at least $h+3$. The offset $p_0$ has height $h$, and $t$ is absent. If $d=0$, take $t=n-1$; the neighbouring-height argument again gives height $h+1$ and absence, and all present offsets are below $t$ numerically. Repeat the window count at this threshold. At most $h$ selected backward arrows have offsets below $t$, since the height-$(h+1)$ table also has an arrow ending at 0. Count $p_0$, $t$, and a possible exceptional $z$ separately, with the same high-column cancellation. Then \[ Z+h+3\le P+h+2+\ind{z\text{ exists}}, \] so $E+1\le\ind{z\text{ exists}}\le1$. This gives $E=0$, contradicting $E=1$. The only possible heights are $h,h+1,h+2$. \subsection{Geometry of an exceptional table} For an exceptional table $q=q_z$, let $F$ be its number of fixed positions, $g$ its gap, and $k=q(0)$. Its height is $h+1$, so \[ H(z+1)=h+1+F-1=h+F. \] The maximum height $h+2$ gives $F\le2$. At least one fixed position exists, namely the last occurrence $j$ of $z$, so $1\le F\le2$. A depth below $h$ by position $g$ would give a short scan by Estimate 2. Thus every depth in $0,\ldots,g$ is at least $h=H-1$. A depth-$(H-1)$ arrow contains as many fixed positions as its offset minus one, so that offset is at most $F+1$. Depth-$H$ cards are fixed and have offset zero. All these prefix offsets are therefore below $F+2$. Subtracting $F+2$ from the labels increases height by at most one, from $h+1$ to at most $h+2$. The height rule gives at most $F+3$ offsets below $F+2$. The prefix supplies $g+1$ such positions, whence $g\le F+2$. The first arrow $0\to k$ contains $k-1$ fixed positions, so $1\le k\le F+1$. All $F$ fixed positions lie between 1 and $j\le g$, so $F\le g$. We have proved \[ 1\le F\le2,\qquad F\le g\le F+2,\qquad1\le k\le F+1. \] In particular $g\le4$. No assumption about the initial height being $h+1$ was needed. \subsection{Excluding a minimum initial height} First exclude event $K$. Under $K$, the initial height is minimum and $d=0$ or 1: for $d>0$, combine $F\ge d+2$ with $F\le3$; for $d=0$, $p_0=0$. Thus $p_0$ is either 0 or $n-1$. The repeated threshold count at $p_0-1$, now with $E=0$, shows that an exceptional $z$ exists. Take its table $q$, gap $g$, first target $k$, and fixed-position count $F$. Suppose the initial height is minimum. Subtracting $k$ from $q$ takes height $h+1$ to $h$, so exactly $k-1$ offsets of $q$ are below $k$. All $F$ fixed positions are counted, and every position $1,\ldots,k-1$ is fixed because the first arrow passes over it. Consequently \[ F=k-1, \] and there are no other offsets below $k$. If $k\le g$, position $k$ belongs to the high-depth prefix and is nonfixed, since it receives the arrow from 0. All fixed positions are less than $k$, so its arrow must go to $k+1$. This supplies offset 1 below $k$, contradicting the completed count. Thus $g0$. The initial height is therefore $h+1$ and its positive gap lies in the window. \subsection{Why the initial gap is 1 or 2} The exceptional-table bounds imply \[ -3\le k-g\le1,\qquad p(0)=\res{k-g}{n}. \] The value $p(0)=0$ would give $d=0$, and $p(0)=1$ would give $d=n-1>h+3$. Both are excluded. Hence $k-g$ is $-1,-2$, or $-3$, so $d=g-k>0$ and $d\rho(j)$. There is one such pair for even size and $m-1$ for odd size, since the relocated card crosses $m-1$ cards. Same-colour pairs are fewest when the colour classes are as equal as possible. For $n=2m$ the minimum is $m(m-1)$; for $n=2m+1$ it is $m^2$. For six positions, a split into three of each colour gives $3+3=6$ pairs. Subtracting the possible exceptions gives \begin{center} \begin{tabular}{lc}\toprule Number of cards&Necessary swaps\\\midrule $n=2m$&at least $m^2-m-1$\\ $n=2m+1$&at least $m^2-m+1$\\\bottomrule \end{tabular} \end{center} This applies to every choice of turns $k_i$. It does not assume that cards use the nearest copies of their target positions. \section{A lower bound on shifts}\label{sec:lower-shifts} Follow a card that must move left. After a swap moving it one place left, the marker is at the card's new position. To move the same card left again, the marker must reach the preceding circular position. As it only advances, this takes $n-1$ shifts. For an arbitrary route, count the first time the card reaches each new position to the left of its start. Extra rightward motion does not remove the need to cross those positions. The same count therefore bounds routes with backtracking. For example, a card going from position 3 to coordinate 0 must cross $3\to2$, $2\to1$, and $1\to0$. With $n=7$, the marker can be at the starts of these successive crossings no earlier than unrolled coordinates 2, 8, and 14. A final shift may still be needed for the required start. Let $\Lambda$ be the total number of shifts. If the card initially at $i$ ends at $T_ij-1=\rho(j)$. These positions have different colours, with turns 0 and 1. The coarse bound subtracted one for every reversed pair, even a different-colour pair that had contributed zero before subtraction. Here the actual contribution is at least one: for opposite colours and reversed order, the floor of the target difference divided by $n$ is an odd integer. We can replace the coarse contribution $-1$ by $+1$, an improvement of two for each of the $K$ pairs. Hence \[ I(v)\ge m^2-m+1+2K=m^2+1-c+\varepsilon. \] The equality $2K=m-c+\varepsilon$ follows by checking the two parities of $c+m$. There are two distinct positions with displacement $\varepsilon-m$. One is in the modified segment: \[ j=\frac{c+m+2-\varepsilon}{2}. \] The other is $j+m$ if $j+m