CodeEff/ECCO huggingface dataset:
# Original solution
N,M = list(map(int,input().split()))
L = [list(map(int,input().split())) for i in range(M)]
L.sort()
ans = 1
l = L[0][0]
r = L[0][1]
for a,b in L:
    if r <= a:
        ans += 1
        l = a
        r = b
    else:
        l = a
        r = min(r,b)
print(ans)


# Optimized solution
N,M = list(map(int,input().split()))
L = [list(map(int,input().split())) for i in range(M)]
L.sort()
ans = 0
l,r = 0,0
for a,b in L:
    l = a
    if r <= a:
        ans += 1
        r = b
    else:
        r = min(r,b)
print(ans)
# Original solution
N,M = list(map(int,input().split()))
L = [list(map(int,input().split())) for i in range(M)]
L.sort()
ans = 0
l,r = 0,0
for a,b in L:
    l = a
    if r <= a:
        ans += 1
        r = b
    else:
        r = min(r,b)
print(ans)


# Optimized solution
N,M = list(map(int,input().split()))
L = sorted([list(map(int,input().split())) for i in range(M)],key = lambda x:x[1])
p,res = 0,0
for a,b in L:
    if a > p:
        p = b-1
        res += 1
print(res)
# Original solution
N,M = list(map(int, input().split()))
table = []
ans = 1

for i in range(M):
  a,b = list(map(int,input().split()))
  table.append((a,b))
  
table.sort()
r = N
for a,b in table:
  if r <= a:
    ans += 1
    r = N
  r = min(b,r)
print(ans)

# Optimized solution
N,M = list(map(int,input().split()))
table = []
for i in range(M):
    a,b = list(map(int,input().split()))
    a -= 1
    b -= 1
    table.append((a,b))
table = sorted(table,key=lambda x:x[1])
cur_r = table[0][1]
ans = 1
for i in range(1,M):
    l,r = table[i]
    if cur_r <= l:
        ans += 1
        cur_r = r

print(ans)
# Original solution
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(): return list(map(int, input().split()))
def ZIP(n): return list(zip(*(MAP() for _ in range(n))))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N, M = MAP()

ab = [LIST() for _ in range(M)]
ab.sort(key = lambda x: (x[0], x[1]))

ans = 0
tmp_l = -INF
tmp_r = -INF
for l, r in ab:
    if tmp_r <= l:
        ans += 1
        tmp_r = r
        tmp_l = l
    if tmp_l < l:
        tmp_l = l
    if r < tmp_r:
        tmp_r = r

print(ans)



# Optimized solution
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(): return list(map(int, input().split()))
def ZIP(n): return list(zip(*(MAP() for _ in range(n))))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N, M = MAP()

ab = [LIST() for _ in range(M)]
ab.sort(key = lambda x: (x[1], x[0]))

ans = 0
tmp = -INF
for l, r in ab:
    if tmp <= l:
        ans += 1
        tmp = r

print(ans)
# Original solution
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(): return list(map(int, input().split()))
def ZIP(n): return list(zip(*(MAP() for _ in range(n))))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N, M = MAP()

ab = [LIST() for _ in range(M)]
ab.sort(key = lambda x: (x[1], x[0]))

ans = 0
tmp = -INF
for l, r in ab:
    if tmp <= l:
        ans += 1
        tmp = r

print(ans)



# Optimized solution
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(): return list(map(int, input().split()))
def ZIP(n): return list(zip(*(MAP() for _ in range(n))))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N, M = MAP()

ab = [LIST() for _ in range(M)]
ab.sort(key = lambda x: x[1])

ans = 0
tmp = -INF
for l, r in ab:
    if tmp <= l:
        ans += 1
        tmp = r

print(ans)
# Original solution
N, M = [int(t) for t in input().split()]
a = [[0]*2 for i in range(M)]  # 始点と距離
for i in range(M):
    s, e = [int(t) for t in input().split()]
    a[i][0] = s - 1
    a[i][1] = e - 1

b = sorted(a, key=lambda x: x[1])

count = 1
flag = b[0][1]
for i in range(1, M):
    if b[i][0] < flag:
        continue
    elif b[i][1] > flag:
        flag = b[i][1]
        count += 1

print(count)


# Optimized solution
N, M = [int(t) for t in input().split()]
a = [[0]*2 for i in range(M)]
for i in range(M):
    a[i][0], a[i][1] = [int(t) for t in input().split()]

b = sorted(a, key=lambda x: x[1])

count = 1
flag = b[0][1]
for i in range(1, M):
    if b[i][0] < flag:
        continue
    elif b[i][1] > flag:
        flag = b[i][1]
        count += 1

print(count)
# Original solution
N, M = list(map(int, input().split()))
kukan = sorted([list(map(int, input().split())) for _ in range(M)], key=lambda x: x[1])
ans = 0

while kukan:
    cut = kukan[0][1] - 1
    kukan = list([x for x in kukan if not x[0] <= cut < x[1]])
    ans += 1

print(ans)

# Optimized solution
def main():
    N, M = list(map(int, input().split()))
    kukan_list = sorted([list(map(int, input().split())) for _ in range(M)], key=lambda x: x[1])
    ans = 0
    cut = 0
    
    for kukan in kukan_list:
        if cut >= kukan[0]:
            continue
        ans += 1
        cut = kukan[1] - 1
    
    print(ans)

if __name__ == "__main__":
    main()
# Original solution
n , m = list(map(int, input().split()))
# ab = [list(map(int, input().split())) for _ in range(m)]
ranges = [[] for _ in range(m)]
for i in range(m):
    a,b = list(map(int,input().split()))
    ranges[i] = [a,b]
ranges = sorted(ranges, key = lambda x:x[1])
MAX = - float("inf")
ans = 0
for i in range(m):
    if MAX < ranges[i][0]:
        MAX = ranges[i][1] - 0.1
        ans += 1
print(ans)

# Optimized solution
n , m = list(map(int, input().split()))
# ab = [list(map(int, input().split())) for _ in range(m)]
ranges = [[] for _ in range(m)]
for i in range(m):
    a,b = list(map(int,input().split()))
    ranges[i] = [a,b]
ranges = sorted(ranges, key = lambda x:x[1])
MAX = - float("inf")
ans = 0
for i in range(m):
    if MAX <= ranges[i][0]:
        MAX = ranges[i][1]
        ans += 1
print(ans)
# Original solution
N, M = list(map(int, input().split()))
ab_s = [list(map(int, input().split())) for _ in range(M)]
bridges = [0] * N
ab_s = sorted(ab_s, key=lambda x: x[1])
for ab in ab_s:
  if sum(bridges[ab[0]:ab[1]]) == 0:
    bridges[ab[1]-1] = 1
print((sum(bridges)))


# Optimized solution
N, M = list(map(int, input().split()))
ab_s = [list(map(int, input().split())) for _ in range(M)]
bridges = [0] * N
ab_s = sorted(ab_s, key=lambda x: x[1])
for ab in ab_s:
    if bridges[ab[1]-1] - bridges[ab[0]-1] == 0:
        for i in range(ab[1]-1, N):
            bridges[i] += 1
print((bridges[-1]))
# Original solution
def g(l, n, v):
    for i in n:
        for j in range(i[0], i[1]):
            l[j] += v

def f(n, m, A):
    global D
    max_n = max(enumerate(D[n:m], n), key=lambda x:x[1])[0]
    dA = {i for i in A if i[0] <= max_n < i[1]}
    g(D, dA, -1)
    return max_n, A - dA

def ff(n, m, A):
    i, A_ = f(n, m, A)
    A_1 = set(j for j in A_ if j[1] <= i)
    A_2 = A_ - A_1

    a1 = ff(n, i, A_1) if A_1 else 0
    a2 = ff(i+1, m, A_2) if A_2 else 0

    return a1 + a2 + 1


w = [int(i) for i in input().split()]
N = w[0]
M = w[1]
A = {tuple([int(j) for j in input().split()]) for i in range(M)}

D = [0] * N
g(D, A, 1)

print((ff(1, N, A)))



# Optimized solution
w = [int(i) for i in input().split()]
N = w[0]
M = w[1]
A = {tuple([int(j) for j in input().split()]) for i in range(M)}

A_ = sorted(A, key=lambda x: x[1])
bridge = 0
c = 0
for i in A_:
    if i[0] > bridge:
        bridge = i[1]-1
        c += 1
print(c)
# Original solution
def main():
    N, M = (int(i) for i in input().split())
    ab = [tuple(int(i) for i in input().split()) for _ in range(M)]
    ab.sort(key=lambda p: p[1])

    pre_b = 1
    ans = 0
    for a, b in ab:
        if pre_b <= a:
            ans += 1
            pre_b = b
    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    import sys
    input = sys.stdin.buffer.readline
    N, M = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(M)]
    AB.sort(key=lambda p: p[1])
    mx_b = AB[0][1]
    ans = 1
    for a, b in AB[1:]:
        if a < mx_b:
            continue
        else:
            mx_b = b
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()
# Original solution

from operator import itemgetter
 
n,m=list(map(int, input().split()))
A=[]
B=[]
a=0
b=0
for i in range(m):
  a,b=list(map(int, input().split()))
  A.append([a,b])

A.sort(key=itemgetter(1))
bri=[1 for i in range(n)] #bri[0]は1,2番目の島をつなぐ

count=0

for i in range(m):
  a=A[i][0]
  b=A[i][1]
  
  if not 0 in bri[a-1:b-1]:
    bri[b-2]=0
    count+=1

print(count)


# Optimized solution

from operator import itemgetter
 
n,m=list(map(int, input().split()))
A=[]
B=[]
a=0
b=0
for i in range(m):
  a,b=list(map(int, input().split()))
  A.append([a,b])

A.sort(key=itemgetter(1))
bri=0 #取った橋のうち番号最大の島のもの。bri=5なら4,5番目の島の橋

count=0

for i in range(m):
  a=A[i][0]
  b=A[i][1]
  
  if a>=bri:
    bri=b
    count+=1

print(count)
# Original solution
def ck(a,b,arr):
  if sum(arr[a:b]) == b - a:
    return 1
  else:
    return 0

N,M=list(map(int,input().split()))
List=[list(map(int,input().split())) for i in range(M)]
List_2 = [0] * M
arr = [1] * N

for i in range(M):
  a = List[i][1]
  b = List[i][0]
  List_2[i] = [a,b]
  
List_2.sort()
  
for i in range(M):
  a = List_2[i][1]
  b = List_2[i][0]

  hantei = ck(a,b,arr)
  if hantei == 1:
    arr[b-1] = 0
  
print((int(N - sum(arr))))

# Optimized solution
def ck(a,b,arr):
  if arr >= a and arr < b:
    return 0
  else:
    return 1

N,M=list(map(int,input().split()))
List=[list(map(int,input().split())) for i in range(M)]
List_2 = [0] * M
arr = 0
cnt = 0

for i in range(M):
  a = List[i][1]
  b = List[i][0]
  List_2[i] = [a,b]
  
List_2.sort()
  
for i in range(M):
  a = List_2[i][1]
  b = List_2[i][0]

  hantei = ck(a,b,arr)
  if hantei == 1:
    arr = b-1
    cnt += 1
  
print((int(cnt)))
# Original solution
if __name__ == '__main__':
	islands_count, demands_count = list(map(int, input().split()))
	demands = []
	bridges = [True for _ in range(islands_count - 1)]

	for i in range(demands_count):
		demand = list(map(int, input().split()))
		demands.append(demand)

	demands.sort(key=lambda x: x[1])

	for demand in demands:
		is_disconnected = False
		for bridge in range(demand[0] - 1, demand[1] - 1):
			if not bridges[bridge]:
				is_disconnected = True
				break
		if not is_disconnected:
			bridges[demand[1] - 2] = False
	print((bridges.count(False)))


# Optimized solution
if __name__ == '__main__':
    islands_count, demands_count = list(map(int, input().split()))
    demands = []
    break_count = 0

    for i in range(demands_count):
        demand = list(map(int, input().split()))
        demands.append(demand)

    demands.sort(key=lambda x: x[1])
    west_edge = 0

    for demand in demands:
        if demand[0] >= west_edge:
            break_count += 1
            west_edge = demand[1]

    print(break_count)
# Original solution
n, m = list(map(int, input().split()))
ab = []
for i in range(m):
    a, b = list(map(int, input().split()))
    ab.append((a - 1, 2, i))
    ab.append((b - 1, 1, i))

ab.sort()
ans = 0
separated = [False] * m
buf = set()
for island, ab, i in ab:
    if ab == 1:
        if separated[i]:
            continue
        else:
            ans += 1
            for j in buf:
                separated[j] = True
            buf.clear()
    else:
        buf.add(i)
print(ans)


# Optimized solution
n, m = list(map(int, input().split()))
disputes = []
for i in range(m):
    a, b = list(map(int, input().split()))
    disputes.append((b, a))
disputes.sort()
ans = 0
most_right_bridge = 0
for b, a in disputes:
    if most_right_bridge <= a:
        ans += 1
        most_right_bridge = b
print(ans)
# Original solution
from collections import defaultdict

n, m = list(map(int, input().split()))
disputes = defaultdict(int)
for i in range(m):
    a, b = list(map(int, input().split()))
    disputes[b] = max(disputes[b], a)
ans = 0
most_right_bridge = 0
for b in sorted(disputes.keys()):
    if most_right_bridge <= disputes[b]:
        ans += 1
        most_right_bridge = b
print(ans)


# Optimized solution
import sys
from collections import defaultdict

n, m = list(map(int, input().split()))
disputes = defaultdict(int)
for line in sys.stdin.readlines():
    a, b = list(map(int, line.split()))
    if disputes[b] < a:
        disputes[b] = a
ans = 0
most_right_bridge = 0
for b in sorted(disputes.keys()):
    if most_right_bridge <= disputes[b]:
        ans += 1
        most_right_bridge = b
print(ans)
# Original solution
N,M=list(map(int,input().split()))
AB=[list(map(int,input().split())) for _ in range(M)]
AB.sort()

ans=0
while AB:
  a=AB[-1][0]
  AB=list([x for x in AB if x[1]<=a])
  ans+=1
  
print(ans)

# Optimized solution
N,M=list(map(int,input().split()))
AB=[list(map(int,input().split())) for _ in range(M)]
AB.sort()

ar=N+1
ans=0
for i in reversed(list(range(M))):
  a,b=AB[i]
  if b<=ar:
    ans+=1
    ar=a
  
print(ans)
# Original solution
# Segment tree
class SegmentTree():
    _data = []
    _offset = 0
    _size = 0

    def __init__(self, size):
        _size = size
        t = 1
        while t < size:
            t *= 2
        self._offset = t - 1
        self._data = [0 for _ in range(t * 2 - 1)]

    def update(self, index, value):
        i = self._offset + index
        self._data[i] = value
        while i >= 1:
            i = (i - 1) // 2
            self._data[i] = self._data[i * 2 + 1] + self._data[i * 2 + 2]

    def query(self, start, stop):
        result = 0
        l = start + self._offset
        r = stop + self._offset
        while l < r:
            if l & 1 == 0:
                result = result + self._data[l]
            if r & 1 == 0:
                result = result + self._data[r - 1]
            l = l // 2
            r = (r - 1) // 2
        return result


N, M = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(M)]

ab.sort(key=lambda x: x[1])
st = SegmentTree(N - 1)

result = 0
for a, b in ab:
    a, b = a - 1, b - 1
    if st.query(a, b) != 0:
        continue
    result += 1
    st.update(b - 1, 1)
print(result)


# Optimized solution
N, M = list(map(int, input().split()))

ab = [[] for _ in range(N + 1)]
for _ in range(M):
    a, b = list(map(int, input().split()))
    ab[a].append(b)

dp = [0] * (N + 1)
for i in range(1, N + 1):
    dp[i] = max(dp[i], dp[i - 1])
    for j in ab[i]:
        dp[j] = max(dp[j], dp[i] + 1)

print((dp[N]))
# Original solution
from sys import stdin
readline = stdin.readline

N, M = list(map(int, readline().split()))

ab = [[] for _ in range(N + 1)]
for _ in range(M):
    a, b = list(map(int, readline().split()))
    ab[a].append(b)

dp = [0] * (N + 1)
for i in range(1, N + 1):
    dp[i] = max(dp[i], dp[i - 1])
    for j in ab[i]:
        dp[j] = max(dp[j], dp[i] + 1)

print((dp[N]))


# Optimized solution
from sys import stdin
readline = stdin.readline

N, M = list(map(int, readline().split()))
ab = [list(map(int, readline().split())) for _ in range(M)]

ab.sort(key=lambda x: x[1])

result = 0
t = -1
for a, b in ab:
    a, b = a - 1, b - 1
    if a <= t < b:
        continue
    result += 1
    t = b - 1
print(result)
# Original solution
# Segment tree (Sum)
class SegmentTree():
    _data = []
    _offset = 0
    _size = 0

    def __init__(self, size):
        _size = size
        t = 1
        while t < size:
            t *= 2
        self._offset = t - 1
        self._data = [0 for _ in range(t * 2 - 1)]

    def update(self, index, value):
        i = self._offset + index
        self._data[i] = value
        while i >= 1:
            i = (i - 1) // 2
            self._data[i] = self._data[i * 2 + 1] + self._data[i * 2 + 2]

    def query(self, start, stop):
        result = 0
        l = start + self._offset
        r = stop + self._offset
        while l < r:
            if l & 1 == 0:
                result = result + self._data[l]
            if r & 1 == 0:
                result = result + self._data[r - 1]
            l = l // 2
            r = (r - 1) // 2
        return result


from sys import stdin
readline = stdin.readline

N, M = list(map(int, readline().split()))
ab = [list(map(int, readline().split())) for _ in range(M)]

ab.sort(key=lambda x: x[1])
st = SegmentTree(N - 1)

result = 0
for a, b in ab:
    a, b = a - 1, b - 1
    if st.query(a, b) != 0:
        continue
    result += 1
    st.update(b - 1, 1)
print(result)


# Optimized solution
from sys import stdin
readline = stdin.readline

N, M = list(map(int, readline().split()))

ab = [[] for _ in range(N + 1)]
for _ in range(M):
    a, b = list(map(int, readline().split()))
    ab[a].append(b)

dp = [0] * (N + 1)
for i in range(1, N + 1):
    dp[i] = max(dp[i], dp[i - 1])
    for j in ab[i]:
        dp[j] = max(dp[j], dp[i] + 1)

print((dp[N]))
# Original solution
# Segment tree (+)
class SegmentTree:
    _f = None
    _data = None
    _offset = None
    _size = None

    def __init__(self, size, f):
        self._f = f
        self._size = size
        t = 1
        while t < size:
            t *= 2
        self._offset = t - 1
        self._data = [0] * (t * 2 - 1)

    def update(self, index, value):
        f = self._f
        data = self._data
        i = self._offset + index
        data[i] = value
        while i >= 1:
            i = (i - 1) // 2
            data[i] = f(data[i * 2 + 1], data[i * 2 + 2])

    def query(self, start, stop):
        def iter_segments(data, l, r):
            while l < r:
                if l & 1 == 0:
                    yield data[l]
                if r & 1 == 0:
                    yield data[r - 1]
                l = l // 2
                r = (r - 1) // 2
        f = self._f
        it = iter_segments(self._data, start + self._offset,
                           stop + self._offset)
        result = next(it)
        for e in it:
            result = f(result, e)
        return result


from sys import stdin
readline = stdin.readline

from operator import add

N, M = list(map(int, readline().split()))
ab = [list(map(int, readline().split())) for _ in range(M)]

ab.sort(key=lambda x: x[1])
st = SegmentTree(N - 1, add)

result = 0
for a, b in ab:
    a, b = a - 1, b - 1
    if st.query(a, b) != 0:
        continue
    result += 1
    st.update(b - 1, 1)
print(result)


# Optimized solution
# Segment tree (+)
from operator import add
from sys import stdin


class SegmentTree:
    _f = None
    _data = None
    _offset = None
    _size = None

    def __init__(self, size, f):
        self._f = f
        self._size = size
        t = 1
        while t < size:
            t *= 2
        self._offset = t - 1
        self._data = [0] * (t * 2 - 1)

    def update(self, index, value):
        f = self._f
        data = self._data
        i = self._offset + index
        data[i] = value
        while i >= 1:
            i = (i - 1) // 2
            data[i] = f(data[i * 2 + 1], data[i * 2 + 2])

    def query(self, start, stop):
        def iter_segments(data, l, r):
            while l < r:
                if l & 1 == 0:
                    yield data[l]
                if r & 1 == 0:
                    yield data[r - 1]
                l = l // 2
                r = (r - 1) // 2
        f = self._f
        it = iter_segments(self._data, start + self._offset,
                           stop + self._offset)
        result = next(it)
        for e in it:
            result = f(result, e)
        return result


readline = stdin.readline

N, M = list(map(int, readline().split()))
ab = [list(map(int, readline().split())) for _ in range(M)]

ab.sort(key=lambda x: x[1])
st = SegmentTree(N - 1, add)

result = 0
for a, b in ab:
    a, b = a - 1, b - 1
    if st.query(a, b) != 0:
        continue
    result += 1
    st.update(b - 1, 1)
print(result)
# Original solution
def main():
    N, M = list(map(int, input().split()))
    ab = [[int(i) for i in input().split()] for _ in range(M)]
    ab = sorted(ab, key=lambda x: x[1])
    bd = ['0'] * (N - 1)
    for a, b in ab:
        if bd[a-1:b-1].count('1') == 0:
            bd[b - 2] = '1'
    print((bd.count('1')))


main()


# Optimized solution
def main():
    N, M = list(map(int, input().split()))
    ab = [[int(i) for i in input().split()] for _ in range(M)]
    ab = sorted(ab, key=lambda x: x[1])
    last = ab[0][1] - 1
    ans = 1
    for a, b in ab[1:]:
        if last >= a:
            continue
        ans += 1
        last = b - 1
    print(ans)


main()