import numpy as np

N = int(input())
p = list(map(int, input().strip().split()))
num = N**2
P = np.ones(num)
ans = 0

for i in range(num):
    a = p[i]
    P[a-1] = i
P = P.reshape([N,N])
# print(P)
for i in range(N-2):
    for j in range(N-2):
        status = False
        status2 = False
        status3 = False
        status4 = False
        st = False
        st2 = False
        for k in range(i+1):
            if P[i+1][j+1] < P[k][j+1]:
                status = True
            if P[i+1][j+1] < P[i+1][k]:
                status2 = True
        for k in range(N-i-2):
            if P[i+1][j+1] < P[k+i+2][j+1]:
                status3 = True
            if P[i+1][j+1] < P[i+1][k+i+2]:
                status4 = True
        if status and status2:
            st = True
        if status3 and status4:
            st2 = True
        if st and st2:
            # print(P[i+1][j+1])
            ans += 1

print(ans)