using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;

public class Program
{

    public void Proc()
    {
        int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();

        this.ShimaCount = inpt[0];

        int youbouCount = inpt[1];

        for (int i = 0; i < youbouCount; i++) {
            inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).OrderBy(a => a).ToArray();
            bundans1.Add(new Bundan(inpt));
            bundans2.Add(new Bundan(inpt));
        }

        bundans2.Sort((a, b) =>
        {
            if (a.Toisl < b.Toisl)
            {
                return -1;
            }
            if (a.Toisl > b.Toisl)
            {
                return 1;
            }
            if (a.FromIsl < b.FromIsl)
            {
                return -1;
            }
            if (a.FromIsl > b.FromIsl)
            {
                return 1;
            }
            return 0;
        });
        bundans1.Sort((a, b) =>
        {
            if (a.FromIsl < b.FromIsl)
            {
                return -1;
            }
            if (a.FromIsl > b.FromIsl)
            {
                return 1;
            }
            if (a.Toisl < b.Toisl)
            {
                return -1;
            }
            if (a.Toisl > b.Toisl)
            {
                return 1;
            }
            return 0;
        });

        int ans = GetAns(1, 0);
        Console.WriteLine(ans);
    }

    private Dictionary<int, int> dic = new Dictionary<int, int>();
    private int GetAns(int pos, int idx) {
        if(pos == this.ShimaCount) {
            return 0;
        }
        if(idx >= this.bundans2.Count) {
            return 0;
        }
        if(dic.ContainsKey(pos)) {
            return dic[pos];
        }

        int subIdx = this.bundans2.Count;
        for (int i = idx; i < this.bundans2.Count; i++) {
            if(this.bundans2[i].FromIsl >= pos) {
                subIdx = i;
                break;
            }
        }

        if(subIdx == this.bundans2.Count) {
            dic[pos] = 0;
            return 0;
        }
        int minBundan = int.MaxValue;

        int tmp = GetAns(this.bundans2[subIdx].Toisl, subIdx) + 1;
        minBundan = Math.Min(minBundan, tmp);
        tmp = GetAns(pos + 1, subIdx) + 1;
        minBundan = Math.Min(minBundan, tmp);
        dic[pos] = minBundan;
        return minBundan;
    }



    List<Bundan> bundans1 = new List<Bundan>();
    List<Bundan> bundans2 = new List<Bundan>(); 
    private class Bundan {
        public int FromIsl;
        public int Toisl;
        public bool IsUsed = false;

        public Bundan(int[] items) {
            this.FromIsl = items.Min();
            this.Toisl = items.Max();
        }
    }


    int ShimaCount;

    public class Reader
    {
        static StringReader sr;
        public static bool IsDebug = false;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (sr == null)
                {
                    sr = new StringReader(InputText.Trim());
                }
                return sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
        private static string InputText = @"


5 2
1 4
2 5



";
    }

    public static void Main(string[] args)
    {
#if DEBUG
        Reader.IsDebug = true;
#endif
        Program prg = new Program();
        prg.Proc();
    }
}

Measured memory_bytes: 35592