I imagined a system of $n$ "things" that are ordered: each "thing" has a specific place. Also, the $n$th thing is the last thing in the system, meaning that there is always a "thing" to which number $1$ is assigned. Given such a system, we can "mess it up" — put things in wrong order. Now we must put them back. Like so (an example):
We see that in this case, we have a system $n = 5$. If all the "things" are in their spots, then the output of the function I wanted to deduce (but failed) is $0$. But if they are not, my "function" adds $1$ to its output for each place a misplaced "thing" must travel to get to its original location. In the above case, the output of function in order to "clean up the mess" is $8$.
The question is: for some system $n$ (where $n \in \mathbb{N}$), what is the maximum of the function $f$?
I already found $f_{\text{max}}$ for $n = 1$ ($f_{\text{max}}(1) = 0$), $n = 2$ ($f_{\text{max}}(2) = 2$) and $n = 3$ ($f_{\text{max}}(3) = 4$). But it is quite clear that this problem needs a different approach; bruteforce won't do any good. So, my main interest is to know what a formula for $f_{\text{max}}(n)$.
I am very eager to see the answer. I tackled a few hard problems, but this one (and the strange thing is that I made it up myself) is one tough cookie, at least for me.

It seems clear that the maximum occurs when you reverse the sequence. Consider the cases $n$ odd and $n$ even separately, and add up the number of positions each item has to move. For example for $n$ odd, the sum is $2(2+4+\,...+\,n-1)$ and for $n$ even it is $2(1+3+5+\,...\,+n-1)$. Work it out to show that for $n$ odd $$f_{odd}(n)={1\over2}(n^2-1)$$ and for $n$ even, $$f_{even}(n)={1\over2}n^2$$