Let $ n $ be a positive integer with decimal representation $ d_k d_{k-1} \dots d_1 d_0 $, where $ k+1 = \text{len}(n) $, $ d_k \ne 0 $, and $ d_i \in \{0,1,\dots,9\} $.
A number is divisible by 25 if and only if its last two digits form a number in the set $ \{00, 25, 50, 75\} $.
We are allowed to perform adjacent swaps, with the constraint that **after every swap**, the resulting number must not have leading zeros (i.e., the first digit must be non-zero).
Let $ L = \text{len}(n) $.
Define the set of valid target suffixes:
$$
T = \{00, 25, 50, 75\}
$$
For each target suffix $ s = ab \in T $, we consider all possible ways to assign two digits in $ n $ to be moved to the last two positions (i.e., positions $ L-2 $ and $ L-1 $) such that:
- The two digits used are distinct occurrences of $ a $ and $ b $ in $ n $ (possibly the same digit if $ a = b = 0 $),
- The remaining digits form a number of length $ L-2 $ with no leading zero (i.e., the leftmost remaining digit must be non-zero).
For each such valid assignment:
- Compute the minimum number of adjacent swaps required to bring the two chosen digits $ a $ and $ b $ to the last two positions, preserving their relative order $ a $ then $ b $ (i.e., $ a $ must end up at position $ L-2 $, $ b $ at $ L-1 $).
- This is computed as the sum of the distances each digit must travel to reach its target position, minus any overlap (i.e., if the left digit moves right past the right digit, their paths cross and the total swaps are reduced by 1).
Additionally, after moving the two digits to the end, the prefix (first $ L-2 $ digits) must not start with zero. So we must ensure that **at least one non-zero digit remains in the prefix**, and the leftmost digit of the prefix is non-zero.
Let $ S $ be the multiset of digits of $ n $.
For each $ s = ab \in T $:
1. Enumerate all pairs of indices $ (i, j) $, $ i < j $, such that $ d_i = a $, $ d_j = b $.
2. For each such pair:
- Temporarily remove $ d_i $ and $ d_j $ from the digit sequence.
- Check if the remaining $ L-2 $ digits have a non-zero first digit. If not, skip.
- Compute the number of adjacent swaps needed to move $ d_i $ to position $ L-2 $ and $ d_j $ to position $ L-1 $, accounting for their relative positions and the fact that moving one affects the other.
- The cost is:
$$
\text{cost} = (L - 1 - j) + (L - 2 - i) - \delta
$$
where $ \delta = 1 $ if $ i < j $ and the movement of $ d_i $ to the left of $ d_j $ causes them to cross (i.e., if $ i $ is to the left of $ j $, and we move $ i $ right past $ j $, then the relative movement is reduced by 1 because $ j $ shifts left by one when $ i $ passes it).
- More precisely:
Let $ \text{pos}_a = i $, $ \text{pos}_b = j $.
After removing both, the number of swaps to bring $ d_i $ to position $ L-2 $ is $ (L - 2 - i) $, but if $ j > i $, then when $ d_i $ moves past $ d_j $, $ d_j $ shifts left by one, so the cost to bring $ d_j $ to $ L-1 $ becomes $ (L - 1 - j - 1) = L - 2 - j $.
So total cost:
$$
(L - 2 - i) + (L - 1 - j) - \mathbf{1}_{i < j}
$$
(since if $ i < j $, moving $ i $ to the right past $ j $ causes $ j $ to move one position left, saving one swap).
3. Among all valid $ s \in T $ and all valid pairs $ (i,j) $, take the minimum cost.
If no valid assignment exists (i.e., for all $ s \in T $, no pair of digits can be moved to the end without leaving a leading zero in the prefix), output $ -1 $.
---
**Formal Statement:**
Let $ n $ be a positive integer with decimal digits $ d_0, d_1, \dots, d_{L-1} $, where $ d_0 \ne 0 $, $ L = \lfloor \log_{10} n \rfloor + 1 $.
Define $ T = \{00, 25, 50, 75\} $.
For each $ s = ab \in T $, and for each pair of indices $ i < j $ such that $ d_i = a $, $ d_j = b $:
- Let $ D' $ be the sequence $ d $ with digits at positions $ i $ and $ j $ removed.
- Let $ d'_0 $ be the first digit of $ D' $. If $ d'_0 = 0 $, skip this pair.
- Compute cost:
$$
c = (L - 2 - i) + (L - 1 - j) - \mathbf{1}_{i < j}
$$
(Note: Since $ i < j $ always in our enumeration, this simplifies to $ 2L - 3 - i - j - 1 = 2L - 4 - i - j $.)
Let $ C = \min \{ c \mid \text{valid } s \in T, \text{valid pair } (i,j) \} $.
If $ C $ is undefined (no valid pair exists), output $ -1 $. Otherwise, output $ C $.
---
**Note:** The formula for cost assumes that moving the left digit $ a $ to position $ L-2 $ requires $ (L - 2 - i) $ swaps, and the right digit $ b $ to position $ L-1 $ requires $ (L - 1 - j) $ swaps, but since $ i < j $, when $ a $ moves past $ b $, $ b $ shifts left by one, so we subtract 1.
Thus, the cost is:
$$
\boxed{2L - 4 - i - j}
$$
for each valid pair $ (i,j) $ with $ d_i = a $, $ d_j = b $, $ a,b \in T $, and the remaining digits form a prefix with non-zero leading digit.