#include<cstdlib>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<set>
#include<vector>
#include<cstring>
#include <algorithm>
#define LL long long
#define inf 0x7fffffff
#define E 1e-9
#define M 310
#define N 505
using namespace std;
int n,h,m,e,t;
int a[N],g[N],b[N];
int solve()
{
    int cnt=0;
    for(int i=1;i<=n;i++)
    if(b[i]!=i)
    {
//        swap(b[i],b[g[i]]);//为啥这样不对?
//        swap(g[i],g[b[i]]);
//        cnt++;


        b[g[i]]=b[i];//这样就对了??
        g[b[i]]=g[i];
        cnt++;
    }
    return cnt;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("ex.in","r",stdin);
#endif
    while(scanf("%d",&n),n)
    {
        for(int i=1;i<=n;++i)
            scanf("%d",&a[i]);
            int minv=n;
        for(int i=1;i<=n;i++)
        {
            for(int j=1,k=i;j<=n;j++,k++)
            {
                if(k>n)
                k=1;
                b[j]=a[k];
                g[a[k]]=j;//好方法!
            }
            int cnt=solve();
            if(cnt<minv)
            minv=cnt;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1,k=i;j<=n;j++,k--)
            {
                if(k==0)
                k=n;
                b[j]=a[k];
                g[a[k]]=j;
            }
            int cnt=solve();
            if(cnt<minv)
            minv=cnt;
        }
        cout<<minv<<endl;


    }
    return 0;
}