答案只有0,1,2,3四种情况:

import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<Integer> v = new ArrayList<>();
        int mx = Integer.MIN_VALUE, mn = Integer.MAX_VALUE;
        for(int i = 0;  i < n; ++i){
            int a = sc.nextInt();
            if(mx < a) mx = a;
            if(mn > a) mn = a;
            v.add(a);
        }
        List<Integer> temp = new ArrayList<>(v);
        Collections.sort(temp);
        if(temp.equals(v)){      //本来就排好序的返回0
            System.out.println(0);
            return;
        }
        if(v.get(v.size() - 1) == mx || v.get(0) == mn){     //最大或最小归位的,1次
            System.out.println(1);
            return;
        }
        int r = 1;
        if(v.get(0) == mx) r++;      //最大值在首位,多挪动1次
        if(v.get(v.size() - 1) == mn) r++;      //最小值在末尾,多挪动1次
        if(v.get(0) != mn && v.get(v.size() - 1) != mx) r++;    //最大最小都在中间
        System.out.println(r);
    }
}