1 #include <bits/stdc++.h> 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 3 #define pb push_back 4 #define is sizeof(int) 5 6 using namespace std; 7 const int maxn = 9; 8 9 int n; 10 int maxd; 11 int v[maxn]; 12 13 int readint() {int tmp;scanf("%d",&tmp);return tmp;} 14 void read() 15 { 16 int vend = 0; 17 _for(i,0,n) 18 v[vend++] = readint(); 19 } 20 21 int wnc()//wrong number count 22 { 23 int cnt = 0; 24 _for(i,1,n) 25 if(v[i-1]+1!=v[i]) 26 cnt ++; 27 return cnt; 28 } 29 30 bool dfs(int d) 31 { 32 if(3*d+wnc()>3*maxd) return false; 33 if(wnc()==0) return true; 34 int vv[maxn]; 35 memcpy(vv,v,sizeof(v)); 36 int tmp[maxn]; 37 _for(i,0,n)//confirm the beginning 38 { 39 if(i==0 || vv[i] != vv[i-1]+1) 40 { 41 _for(j,i,n) 42 { 43 while(j+1<n && vv[j+1]==vv[j]+1) j++; 44 memcpy(tmp,vv+i,is*(j-i+1));//move part 45 _for(k,j+1,n) 46 { 47 while(k+1<n && vv[k+1]==vv[k]+1) k++; 48 memcpy(v+i,vv+j+1,is*(k-j)); 49 memcpy(v+i+(k-j),tmp,is*(j-i+1)); 50 if(dfs(d+1))return true; 51 memcpy(v,vv,sizeof(v)); 52 } 53 } 54 } 55 } 56 return false; 57 } 58 59 int solve() 60 { 61 if(wnc()==0) return 0; 62 for(maxd = 1; ;maxd ++) 63 { 64 if(dfs(0)) break; 65 } 66 return maxd; 67 } 68 69 int kase = 1; 70 void output(int rnt) 71 { 72 printf("Case %d: %d ",kase++,rnt); 73 } 74 75 int main() 76 { 77 while(~scanf("%d",&n)&&n!=0) 78 { 79 memset(v,0,sizeof(v)); 80 read(); 81 output(solve()); 82 } 83 return 0; 84 }