Problem:
Score : 100 points

Problem Statement

You have N cups and 1 ball.

The cups are arranged in a row, from left to right.

You turned down all the cups, then inserted the ball into the leftmost cup.

Then, you will perform the following Q operations:

The i-th operation: swap the positions of the A_i-th and B_i-th cups from the left. If one of these cups contains the ball, the ball will also move.

Since you are a magician, you can cast a magic described below:

Magic: When the ball is contained in the i-th cup from the left, teleport the ball into the adjacent cup (that is, the (i-1)-th or (i+1)-th cup, if they exist).

The magic can be cast before the first operation, between two operations, or after the last operation, but you are allowed to cast it at most once during the whole process.

Find the number of cups with a possibility of containing the ball after all the operations and possibly casting the magic.

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

1 \leq A_i < B_i \leq N

Input

The input is given from Standard Input in the following format:

N Q
A_1 B_1
A_2 B_2
:
A_Q B_Q

Output

Print the number of cups with a possibility of eventually containing the ball.

Sample Input 1

10 3
1 3
2 4
4 5

Sample Output 1

4

Sample Input 2

20 3
1 7
8 20
1 19

Sample Output 2

5

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
 
public class Program
{
    public void Proc() {
        Reader.IsDebug = false;
        int[] inpt = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray();
        this.CupCount = inpt[0];
        int swapCount = inpt[1];
        for(int i=0; i<swapCount; i++) {
            int[] mv = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray();
            this.Move.Add(i, mv);
        }
        SetAns(0, 1, true);
        SetAns(0, 2, false);
        Console.WriteLine(ans.Count);
    }

    private int CupCount;
    private Dictionary<int, int[]> Move = new Dictionary<int, int[]>();

    private Dictionary<int, Dictionary<int, Dictionary<bool, bool>>> flags = new Dictionary<int, Dictionary<int, Dictionary<bool, bool>>>();

    private Dictionary<int, bool> ans = new Dictionary<int, bool>();

    private void SetAns(int idx, int pos, bool canMagicUse) {
        if(!flags.ContainsKey(idx)) {
            flags.Add(idx, new Dictionary<int, Dictionary<bool, bool>>());
        }
        if(!flags[idx].ContainsKey(pos)) {
            flags[idx].Add(pos, new Dictionary<bool, bool>());
        }
        if(flags[idx][pos].ContainsKey(canMagicUse)) {
            return;
        }
        flags[idx][pos][canMagicUse] = true;

        if(idx >= this.Move.Count) {
            this.ans[pos] = true;
            if(canMagicUse) {
                if(pos > 1) {
                    this.ans[pos-1] = true;
                }
                if(pos < this.CupCount) {
                    this.ans[pos+1] = true;
                }
            }
            return;
        }

        int currentPos = pos;
        if(Move[idx][0] == pos) {
            currentPos = Move[idx][1];
        } else if(Move[idx][1] == pos) {
            currentPos = Move[idx][0];
        }
        this.SetAns(GetNextIndex(idx, currentPos), currentPos, canMagicUse);
        if(canMagicUse) {
            if(currentPos > 1) {
                this.SetAns(this.GetNextIndex(idx, currentPos-1),currentPos - 1, false );
            }
            if(currentPos < this.CupCount) {
                this.SetAns(this.GetNextIndex(idx, currentPos+1), currentPos +1, false);
            }
        }
    }

    private int GetNextIndex(int currentIdx, int pos) {
        KeyValuePair<int, int[]> item = Move.FirstOrDefault(a=>a.Key > currentIdx && a.Value.Any(b=>b==pos));
        if(item.Value == null) {
            return this.Move.Count;
        }
        return item.Key;
    }
    public class Reader {
        public static bool IsDebug = true;
        private static System.IO.StringReader SReader;
        private static string InitText = @"


20 1
1 7
8 20
1 19


";
        public static string ReadLine() {
            if(IsDebug) {
                if(SReader == null) {
                    SReader = new System.IO.StringReader(InitText.Trim());
                }
                return SReader.ReadLine();
            } else {
                return Console.ReadLine();
            }
        }
    }
    public static void Main(string[] args)
    {
        Program prg = new Program();
        prg.Proc();
    }
}