import java.util.Scanner; /** * QQ11 小Q的排序 * @author d3y1 */ public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ solution(in); } } /** * 模拟法: 穷举所有情形 * @param in */ private static void solution(Scanner in){ int n = in.nextInt(); int[] nums = new int[n]; int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; boolean isSorted = true; for(int i=0; i<n; i++){ nums[i] = in.nextInt(); if(min > nums[i]){ min = nums[i]; } if(max < nums[i]){ max = nums[i]; } if(i+1 < n){ if(nums[i] > nums[i+1]){ isSorted = false; } } } int result; if(isSorted){ result = 0; }else if(min==nums[0] || max==nums[n-1]){ result = 1; }else if(min==nums[n-1] && max==nums[0]){ result = 3; }else{ result = 2; } System.out.println(result); } }