#include <iostream>
#include <cmath>
struct ULong2
{
uint64_t x, y;
};
uint64_t GetSpiralValue(int size, const ULong2& pos)
{
// Distance form center for each axis
ULong2 distance =
{
std::abs(pos.x >= size / 2 ? pos.x - size / 2 : pos.x - size / 2 + 1),
std::abs(pos.y >= size / 2 ? pos.y - size / 2 : pos.y - size / 2 + 1)
};
// Maximum distance from the center tells witch ring it is.
// Maybe can be optimized, but no.
int ringFromCenter = std::max(distance.x, distance.y);
int ringInToCenter = (ringFromCenter <= size / 2 ? size / 2 - ringFromCenter : (size / 2 + 1) - ringFromCenter) - 1;
// Can be optimized as its a linear sum (I think).
uint64_t ringStartPoint = 1 + (uint64_t)size* (uint64_t)size - std::pow(2 * ringFromCenter + 2, 2);
ULong2 RelativePos =
{
pos.x - ringInToCenter,
pos.y - ringInToCenter,
};
if (RelativePos.x == 0 && RelativePos.y == 0)
return ringStartPoint;
if (RelativePos.x == 0)
return ringStartPoint + RelativePos.y;
if (RelativePos.y == 2 * ringFromCenter + 1)
return ringStartPoint + RelativePos.x + 2 * ringFromCenter + 1;
if (RelativePos.x == 2 * ringFromCenter + 1)
return ringStartPoint - RelativePos.y + 2*(2 * ringFromCenter + 2) + (2 * ringFromCenter) - 1;
if (RelativePos.y == 0)
return ringStartPoint - RelativePos.x + 2 * (2 * ringFromCenter + 2) + 2 * (2 * ringFromCenter);
return 0;
}
int main()
{
int n, t;
std::cin >> n;
std::cin >> t;
ULong2* positions = new ULong2[t];
for (int i = 0; i < t; i++)
{
uint64_t x, y;
std::cin >> x;
std::cin >> y;
positions[i] = { y - 1, x - 1 };
}
for (int i = 0; i < t; i++)
{
auto& pos = positions[i];
std::cout << GetSpiralValue(n, pos) << "\n";
}
delete[] positions;
//for (int y = 0; y < n; y++)
//{
// for (int x = 0; x < n; x++)
// {
// std::cout << GetSpiralValue(n, { x, y }) << " ";
// }
// std::cout << "\n";
//}
}