//
// Written by Fyodor Menshikov 24.02.2009
// 10:38-10:49
//

import java.io.*;
import java.util.*;

/*class Scanner {

   StreamTokenizer in;

   Scanner(Reader r) {
      in = new StreamTokenizer(new BufferedReader(r));
   }

   void asserT(boolean e) {
      if (!e) {
         throw new Error();
      }
   }

   int nextInt() {
      try {
         in.nextToken();
         asserT(in.ttype == in.TT_NUMBER);
         int value = (int)in.nval;
         asserT(value == in.nval);
         return value;
      } catch (IOException e) {
         throw new Error(e);
      }
   }
}*/

public class Main {

   Scanner in;
   PrintWriter out;

   void asserT(boolean e) {
      if (!e) {
         throw new Error();
      }
   }

   void shift(int a[]) {
      int a0 = a[0];
      for (int i = 1; i < a.length; i++) {
         a[i - 1] = a[i];
      }
      a[a.length - 1] = a0;
   }

   void reverse(int a[]) {
      int i1 = 0;
      int i2 = a.length - 1;
      while (i1 < i2) {
         int t = a[i1];
         a[i1] = a[i2];
         a[i2] = t;
         i1++;
         i2--;
      }
   }

   int count(int a[]) {
      a = a.clone();
      int nLoops = 0;
      for (int i = 0; i < a.length; i++) {
         if (a[i] >= 0) {
            int j = i;
            while (a[j] >= 0) {
               int t = a[j];
               a[j] = -1;
               j = t;
            }
            nLoops++;
         }
      }
      return a.length - nLoops;
   }

   void solve() {
      int a[] = new int[in.nextInt()];
      boolean present[] = new boolean[a.length];
      for (int i = 0; i < a.length; i++) {
         a[i] = in.nextInt() - 1;
         asserT(!present[a[i]]);
         present[a[i]] = true;
      }
      int minCount = Integer.MAX_VALUE;
      for (int i = 0; i < a.length; i++) {
         shift(a);
         minCount = Math.min(minCount, count(a));
      }
      reverse(a);
      for (int i = 0; i < a.length; i++) {
         shift(a);
         minCount = Math.min(minCount, count(a));
      }
      asserT(minCount < Integer.MAX_VALUE);
      out.println(minCount);
   }

   void run() {
      try {
         in = new Scanner(System.in);
         out = new PrintWriter(System.out);
      } catch (Exception e) {
         throw new Error(e);
      }
      try {
         solve();
      } finally {
         out.close();
      }
   }

   public static void main(String args[]) {
      new Main().run();
   }
}