using System;
using System.Collections.Generic;

namespace AOJ0556Tile2
{
    public class Program
    {
        static void Main(string[] args)
        {
            var inputData = GetInputData();

            var blankedTileColors = IdentifyBlankedTileColors(inputData);

            OutputBlankedTileColors(blankedTileColors);
        }

        private static InputData GetInputData()
        {
            var pictureSideLength = int.Parse(Console.ReadLine());
            var blankedTileCount = int.Parse(Console.ReadLine());

            var blankedTilePositions = new Point[blankedTileCount];

            for (var i = 0; i < blankedTileCount; ++i)
            {
                var blankedTilePosition = Console.ReadLine().Split(' ');
                blankedTilePositions[i] = new Point(int.Parse(blankedTilePosition[0]), int.Parse(blankedTilePosition[1]));
            }

            return new InputData(pictureSideLength, blankedTilePositions);
        }

        private static IEnumerable<int> IdentifyBlankedTileColors(InputData inputData)
        {
            var picture = new Picture(inputData.PictureSideLength);
            var blankedTileColors = new List<int>();

            foreach (var blankedTilePosition in inputData.BlankedTilePositions)
            {
                blankedTileColors.Add(picture.GetTileColor(blankedTilePosition));
            }

            return blankedTileColors;
        }

        private static void OutputBlankedTileColors(IEnumerable<int> blankedTileColors)
        {
            foreach (var blankedTileColor in blankedTileColors)
            {
                Console.WriteLine(blankedTileColor);
            }
        }
        
        private class InputData
        {
            public int PictureSideLength { get; private set; }
            public IEnumerable<Point> BlankedTilePositions { get; private set; }

            public InputData(int pictureSideLength, IEnumerable<Point> blankedTilePositions)
            {
                this.PictureSideLength = pictureSideLength;
                this.BlankedTilePositions = blankedTilePositions;
            }
        }
    }

    public class Picture
    {
        public int SideLength { get; private set; }

        public Picture(int sideLength)
        {
            this.SideLength = sideLength;
        }

        // 1:Red, 2:Blue, 3:Yellow
        public int GetTileColor(Point tilePosition)
        {
            var distanceFromClosestSide
                = CalculateDistanceFromClosestSide(tilePosition);

            return (distanceFromClosestSide % 3) + 1;
        }

        private int CalculateDistanceFromClosestSide(Point tilePosition)
        {
            var minimumDistanceFromHorizontalSides
                = Math.Min(tilePosition.ai - 1, this.SideLength - tilePosition.ai);

            var minimumDistanceFromVerticalSides
                = Math.Min(tilePosition.bi - 1, this.SideLength - tilePosition.bi);

            return Math.Min(minimumDistanceFromHorizontalSides, minimumDistanceFromVerticalSides);
        }
    }

    public class Point
    {
        public int ai { get; private set; }
        public int bi { get; private set; }

        public Point(int x, int y)
        {
            this.ai = x;
            this.bi = y;
        }
    }
}



Measured memory_bytes: 19968