#!/usr/bin/env python3



from collections import defaultdict



def main():

    n, m = list(map(int, input().split()))

    a = []

    b = []

    atoi = defaultdict(list)

    btoi = defaultdict(list)

    for i in range(m):

        aa, bb = list(map(int, input().split()))

        a.append(aa - 1)

        b.append(bb - 1)

        atoi[aa - 1].append(i)

        btoi[bb - 1].append(i)



    achieved = [False for i in range(m)]

    res = 0

    aptr = -1  # Already achieved

    for x in range(n - 1):

        tobreak = False

        for i in btoi[x + 1]:

            if not achieved[i]:

                tobreak = True

        if tobreak:

            res += 1

            while aptr < x:

                aptr += 1

                for j in atoi[aptr]:

                    achieved[j] = True

    print(res)



if __name__ == "__main__":

    main()

#!/usr/bin/env python3



import sys

from collections import defaultdict



def main():

    n, m = list(map(int, input().split()))

    a = []

    b = []

    atoi = defaultdict(list)

    btoi = defaultdict(list)

    for i in range(m):

        aa, bb = list(map(int, sys.stdin.readline().split()))

        a.append(aa - 1)

        b.append(bb - 1)

        atoi[aa - 1].append(i)

        btoi[bb - 1].append(i)



    achieved = [False for i in range(m)]

    res = 0

    aptr = -1  # Already achieved

    for x in range(n - 1):

        tobreak = False

        for i in btoi[x + 1]:

            if not achieved[i]:

                tobreak = True

        if tobreak:

            res += 1

            while aptr < x:

                aptr += 1

                for j in atoi[aptr]:

                    achieved[j] = True

    print(res)



if __name__ == "__main__":

    main()

#!/usr/bin/env python3
from collections import defaultdict


def main():
    n, m = list(map(int, input().split()))
    a = []
    b = []
    atoi = defaultdict(list)
    btoi = defaultdict(list)
    for i in range(m):
        aa, bb = list(map(int, input().split()))
        a.append(aa - 1)
        b.append(bb - 1)
        atoi[aa - 1].append(i)
        btoi[bb - 1].append(i)
    achieved = [False for i in range(m)]
    res = 0
    aptr = -1  # Already achieved
    for x in range(n - 1):
        tobreak = False
        for i in btoi[x + 1]:
            if not achieved[i]:
                tobreak = True
        if tobreak:
            res += 1
            while aptr < x:
                aptr += 1
                for j in atoi[aptr]:
                    achieved[j] = True
    print(res)


if __name__ == "__main__":
    main()

#!/usr/bin/env python3
import sys
from collections import defaultdict


def main():
    n, m = list(map(int, input().split()))
    a = []
    b = []
    atoi = defaultdict(list)
    btoi = defaultdict(list)
    for i in range(m):
        aa, bb = list(map(int, sys.stdin.readline().split()))
        a.append(aa - 1)
        b.append(bb - 1)
        atoi[aa - 1].append(i)
        btoi[bb - 1].append(i)
    achieved = [False for i in range(m)]
    res = 0
    aptr = -1  # Already achieved
    for x in range(n - 1):
        tobreak = False
        for i in btoi[x + 1]:
            if not achieved[i]:
                tobreak = True
        if tobreak:
            res += 1
            while aptr < x:
                aptr += 1
                for j in atoi[aptr]:
                    achieved[j] = True
    print(res)


if __name__ == "__main__":
    main()

+import sys -        aa, bb = list(map(int, input().split())) +        aa, bb = list(map(int, sys.stdin.readline().split()))