#include<iostream> int m,n; int *p; int dis=0; int peo=0; int data[3][2]; int dx[2]={-1,1}; int a[6][3]={{1,2,3},{1,3,2},{2,3,1},{2,1,3},{3,1,2},{3,2,1}}; int l=1; int r=1; int min=1000000; int main() { freopen("input.txt","r",stdin); scanf("%d",&m); printf("!!"); for(int i=0;i<3;i++) { for(int j=0;j<2;j++) { scanf("%d",&data[i][j]); //first position second people } } // for(int i=0;i<3;i++) //{ for(int j=0;j<2;j++) // { // printf("%d",data[i][j]); //first position second people // } // } p=new int[m]; /*for(int i=0;i<m;i++) { printf("%d",p[i]); }*/ for(int i=0;i<6;i++) { l=1;r=1;dis=0; for(int j=0;j<3;j++) { while(data[a[i][j]-1][1]--) { printf("%d",data[a[i][j]-1][1]); if(p[data[a[i][j]-1][0]]!=1) { p[data[a[i][j]-1][0]]=1; dis++; peo++; } if(p[data[a[i][j]][0]-1]+l*dx[0]>=0&&p[data[a[i][j]][0]-1]+l*dx[0]<m&&p[data[a[i][j]-1][0]]+l*dx[0]!=1) dis=dis+l+1; peo++; l++; if(peo==data[a[i][j]-1][1]) break; if(p[data[a[i][j]-1][0]]+r*dx[1]>=0&&p[data[a[i][j]-1][0]]+r*dx[1]<m&&p[data[a[i][j]-1][0]]+r*dx[1]!=1) dis=dis+r+1; peo++; r++; if(peo==data[a[i][j]-1][1]) break; } } printf("%d\n",dis); if(dis<min) { min=dis; } } printf("!!"); printf("%d",min); }