Suppose $x$ factorises and has $p^r$ as the highest power of the prime $p$. The cycle decomposition of the element $e$ needs to have a component of length $p^r$. If $p$ is $2$ you need to add a transposition to make the element even rather than odd.
This works because $(a-1)(b-1)\gt 0 \implies ab\ge a+b$ with $ab\gt a+b$ in the cases of interest - where $a$ and $b$ are powers of different primes.
So for $18$ you would need a nine-cycle and two transpositions to give $n=13$. For $15$ you could do a three cycle and a five cycle with $n=8$.
For $4$ the answer is $6$ with a four-cycle and a transposition.
So the way this works is this. An element $e$ of a symmetric or alternating group has a decomposition into disjoint cycles. The order of the element is the least common multiple of the orders of the cycles.
Here note that the cycles permute different elements of the underlying set, so they commute. Suppose the cycles are $a, b, c$ for example, with orders $d, e, f$ whose least common multiple is $m$. We have $(abc)^m=a^mb^mc^m=1$. And suppose there is some $k$ with $k\lt m$ and $(abc)^k=1$, then at least one of $d,e,f$ is not a factor of $k$, say $d$. Then $a^k=b^{m-k}c^{m-k}\neq 1$ expresses $a^k$ as a product of powers of $b$ and $c$, but this is absurd because they move disjoint subsets of the underlying set and can't be equal.
To minimise the total length of the cycles for an element of order $x$ we make them pairwise coprime.
Reason [amended in edit] - if we have cycles of length $ab$ and $ac$, where $b$ and $c$ are coprime and $a\gt 1$, let $d$ be the highest common factor of $a$ and $b$ and $a=de$. We then have that $d$ and $c$ are coprime, and also that $b$ and $e$ are coprime so that $bd$ and $ce$ are coprime. If we now use cycles of length $bd$ and $ce$ we have that $bd+ce\lt ab+ac$ and $bcde=abc$.
For order $p^r$ with $p$ a prime we can't do better than a cycle of length $p^r$ permuting $p^r$ elements.
This means that if we have a target order $$x=\prod p_i^{r_i} $$ where the primes $p_i$ are the distinct primes which divide $x$, we can split into disjoint cycles of coprime length $p_i^{r_i}$ which permute at least $$\sum p_i^{r_i}$$elements, where the sum is over the distinct primes dividing $x$ with their respective exponents. In the symmetric group, this is sufficient. If one of the $p_i=2$, there will be one cycle of even length, which is therefore an odd permutation. If we want to be in the alternating group we have to multiply by a further transposition in this case, and add $2$ to the sum. We can do this, if we want to as $$n=\sum \left(p_i^{r_i}+(-1)^{p_i}+1\right)$$ .