/*
#include <stdio.h>
#include <algorithm>
using namespace std;
long long int n, v[500010]={},a=0,b=0;
int main(){
scanf("%lld",&n);
for(int i = 1; i <= n; i++){
scanf("%lld",&v[i]);
}
for (int i = n; i > 0; i--) {
if(v[i] > a) {
a++;
b += a;
}
else{
b += v[i];
a = v[i];
}
}
printf("%lld", b);
return 0;
}
*/
/*
#include <stdio.h>
#include <algorithm>
#include <queue>
using namespace std;
using ii = pair<int, int>;
int main(){
int n,k,arr[60][60]={}, v[60][60]={};
scanf("%d%d",&n,&k);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
scanf("%d",&arr[i][j]);
}
}
if(k==0){
if(arr[1][1]!=5 && arr[1][1]!=1){
printf("-1");
}
else if(arr[n][n]!=2 && arr[n][n] != 5){
printf("-1");
}
else{
queue<int> qx, qy, qi;
qx.push(1);
qy.push(1);
qi.push(4);
v[1][1] = 1;
while(!qi.empty()){
int a = qx.front(), b = qy.front(), t = qi.front();
qx.pop();
qy.pop();
qi.pop();
if(a > n || b > n || a < 1 || b < 1) continue;
if(a == n && b == n) break;
if(t==1){
if(arr[a][b]==2){
if(arr[a][b+1] % 2 == 1){
if(v[a][b+1]==0){
v[a][b+1] = v[a][b] + 1;
qx.push(a);
qy.push(b+1);
qi.push(4);
}
}
}
if(arr[a][b]==3){
if(arr[a][b-1] == 0 || arr[a][b-1] == 2 || arr[a][b-1] == 5){
if(v[a][b-1]==0){
v[a][b-1] = v[a][b] + 1;
qx.push(a);
qy.push(b-1);
qi.push(2);
}
}
}
if(arr[a][b]==4){
if(arr[a+1][b] >= 2 && arr[a+1][b] <= 4){
if(v[a+1][b]==0){
v[a+1][b] = v[a][b] + 1;
qx.push(a+1);
qy.push(b);
qi.push(1);
}
}
}
}
if(t==2){
if(arr[a][b]==0){
if(arr[a+1][b] >= 2 && arr[a+1][b] <= 4){
if(v[a+1][b]==0){
v[a+1][b] = v[a][b] + 1;
qx.push(a+1);
qy.push(b);
qi.push(1);
}
}
}
if(arr[a][b]==2){
if(arr[a-1][b]== 0 || arr[a-1][b] == 1 || arr[a-1][b] == 4){
if(v[a-1][b]==0){
v[a-1][b] = v[a][b] + 1;
qx.push(a-1);
qy.push(b);
qi.push(3);
}
}
}
if(arr[a][b]==5){
if(arr[a][b-1] == 0 || arr[a][b-1] == 2 || arr[a][b-1] == 5){
if(v[a][b-1]==0){
v[a][b-1] = v[a][b] + 1;
qx.push(a);
qy.push(b-1);
qi.push(2);
}
}
}
}
if(t==3){
if(arr[a][b]==0){
if(arr[a][b+1] == 1 || arr[a][b+1] == 3 || arr[a][b+1] == 5){
if(v[a][b+1]==0){
v[a][b+1] = v[a][b] + 1;
qx.push(a);
qy.push(b+1);
qi.push(4);
}
}
}
if(arr[a][b]==1){
if(arr[a][b-1] == 0 || arr[a][b-1] == 2 || arr[a][b-1] == 5){
if(v[a][b-1]==0){
v[a][b-1] = v[a][b] + 1;
qx.push(a);
qy.push(b-1);
qi.push(2);
}
}
}
if(arr[a][b]==4){
if(arr[a-1][b]== 0 || arr[a-1][b] == 1 || arr[a-1][b] == 4){
if(v[a-1][b]==0){
v[a-1][b] = v[a][b] + 1;
qx.push(a-1);
qy.push(b);
qi.push(3);
}
}
}
}
if(t==4){
if(arr[a][b]==1){
if(arr[a+1][b] >= 2 && arr[a+1][b] <= 4){
if(v[a+1][b]==0){
v[a+1][b] = v[a][b] + 1;
qx.push(a+1);
qy.push(b);
qi.push(1);
}
}
}
if(arr[a][b]==3){
if(arr[a-1][b]== 0 || arr[a-1][b] == 1 || arr[a-1][b] == 4){
if(v[a-1][b]==0){
v[a-1][b] = v[a][b] + 1;
qx.push(a-1);
qy.push(b);
qi.push(3);
}
}
}
if(arr[a][b]==5){
if(arr[a][b+1] % 2 == 1){
if(v[a][b+1]==0){
v[a][b+1] = v[a][b] + 1;
qx.push(a);
qy.push(b+1);
qi.push(4);
}
}
}
}
}
if(v[n][n] != 0) printf("%d",v[n][n]);
else printf("-1");
}
}
return 0;
}
*/