#include "bits/stdc++.h"
#include <boost/multi_array.hpp>
#include <boost/optional.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/dynamic_bitset.hpp>
namespace adaptor = boost::adaptors;
using index_t = std::size_t;

namespace lib
{
	using graph_t = std::vector<std::set<index_t>>;
	template<class T>using weighted_graph_t = std::vector<std::map<index_t, T>>;

	namespace impl
	{
		struct max_t
		{
			template<class T>constexpr T operator()(T&& lhs, T&& rhs)const
			{
				return std::max(lhs, rhs);
			}
		};
		struct min_t
		{
			template<class T>constexpr T operator()(T&& lhs, T&& rhs)const
			{
				return std::min(lhs, rhs);
			}
		};
	}
	constexpr impl::max_t max;
	constexpr impl::min_t min;

	template<class T>auto orderby(T func)
	{
		return [=](auto&& lhs, auto&& rhs)
		{
			return func(lhs) < func(rhs);
		};
	}

	template<class T>constexpr auto max_v = std::numeric_limits<T>::max();
	template<class T>constexpr auto min_v = std::numeric_limits<T>::min(); 

	template<class T>using p_queue = std::priority_queue<T, std::vector<T>, std::greater<>>;
}

void Main()
{
	int N;
	std::cin >> N;
	auto decode = [&](int idx)
	{
		--idx;
		return std::make_pair(idx / N, idx % N);
	};
	std::vector<std::pair<int, int>> vec;
	for (auto _ : boost::irange(N * N))
	{
		int code;
		std::cin >> code;
		vec.push_back(decode(code));
	}
	boost::multi_array<bool, 2> opened(boost::extents[N][N]), checked(boost::extents[N][N]);
	std::deque<std::tuple<int, int, int>> queue;
	int count = 0;
	auto push = [&](int a, int b, int c)
	{
		if (0 <= a && a < N && 0 <= b && b < N && !checked[a][b])
		{
			checked[a][b] = true;
			if (opened[a][b])
			{
				queue.emplace_front(a, b, c);
			}
			else
			{
				queue.emplace_back(a, b, c + 1);
			}
		}
	};
	for (auto [i, k] : vec)
	{
		for (auto x : boost::irange(N))
		{
			for (auto y : boost::irange(N))
			{
				checked[x][y] = false;
			}
		}
		opened[i][k] = true;
		push(i, k, 0);
		while (queue.size())
		{
			auto p = queue.front();
			auto [a, b, c] = p;
			queue.pop_front();
			if (a == 0 || a == N - 1 || b == 0 || b == N - 1)
			{
				count += c;
				break;
			}
			push(a - 1, b, c);
			push(a + 1, b, c);
			push(a, b - 1, c);
			push(a, b + 1, c);
		}
		queue.clear();
	}
	std::cout << count << std::endl;
}

int main()
{
	std::cin.tie(nullptr);
	std::ios_base::sync_with_stdio(false);
	std::cout << std::fixed << std::setprecision(15);
	Main();
}