#!/usr/bin/env python3
# Generated by AtCoder Tools 2.8.0
# Copyright 2021 Yuichiro Smith
import sys
from bisect import bisect_left


def solve(N: int, M: int, a: "List[int]", b: "List[int]"):
    key_f = (lambda pair: (pair[0], -pair[1]))
    pairs = [
        (key_f((a, b)), a, b)
        for a, b in zip(a, b)
    ]
    pairs.sort()
    INF = N+1
    lis = [INF] * M
    max_idx = 0
    for _, _, key in pairs:
        idx = bisect_left(lis, key)
        max_idx = max(max_idx, idx)
        lis[idx] = key
    print(max_idx+1)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    a = [int()] * (M)  # type: "List[int]"
    b = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, M, a, b)


if __name__ == '__main__':
    main()

9161819df162b63c430323dd2ffa217f744f36de
<mask token>


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))
    M = int(next(tokens))
    a = [int()] * M
    b = [int()] * M
    for i in range(M):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, M, a, b)


<mask token>

<mask token>


def solve(N: int, M: int, a: 'List[int]', b: 'List[int]'):
    key_f = lambda pair: (pair[0], -pair[1])
    pairs = [(key_f((a, b)), a, b) for a, b in zip(a, b)]
    pairs.sort()
    INF = N + 1
    lis = [INF] * M
    max_idx = 0
    for _, _, key in pairs:
        idx = bisect_left(lis, key)
        max_idx = max(max_idx, idx)
        lis[idx] = key
    print(max_idx + 1)


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))
    M = int(next(tokens))
    a = [int()] * M
    b = [int()] * M
    for i in range(M):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, M, a, b)


<mask token>

<mask token>


def solve(N: int, M: int, a: 'List[int]', b: 'List[int]'):
    key_f = lambda pair: (pair[0], -pair[1])
    pairs = [(key_f((a, b)), a, b) for a, b in zip(a, b)]
    pairs.sort()
    INF = N + 1
    lis = [INF] * M
    max_idx = 0
    for _, _, key in pairs:
        idx = bisect_left(lis, key)
        max_idx = max(max_idx, idx)
        lis[idx] = key
    print(max_idx + 1)


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))
    M = int(next(tokens))
    a = [int()] * M
    b = [int()] * M
    for i in range(M):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, M, a, b)


if __name__ == '__main__':
    main()

import sys
from bisect import bisect_left


def solve(N: int, M: int, a: 'List[int]', b: 'List[int]'):
    key_f = lambda pair: (pair[0], -pair[1])
    pairs = [(key_f((a, b)), a, b) for a, b in zip(a, b)]
    pairs.sort()
    INF = N + 1
    lis = [INF] * M
    max_idx = 0
    for _, _, key in pairs:
        idx = bisect_left(lis, key)
        max_idx = max(max_idx, idx)
        lis[idx] = key
    print(max_idx + 1)


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))
    M = int(next(tokens))
    a = [int()] * M
    b = [int()] * M
    for i in range(M):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, M, a, b)


if __name__ == '__main__':
    main()

#!/usr/bin/env python3
# Generated by AtCoder Tools 2.8.0
# Copyright 2021 Yuichiro Smith
import sys
from bisect import bisect_left


def solve(N: int, M: int, a: "List[int]", b: "List[int]"):
    key_f = (lambda pair: (pair[0], -pair[1]))
    pairs = [
        (key_f((a, b)), a, b)
        for a, b in zip(a, b)
    ]
    pairs.sort()
    INF = N+1
    lis = [INF] * M
    max_idx = 0
    for _, _, key in pairs:
        idx = bisect_left(lis, key)
        max_idx = max(max_idx, idx)
        lis[idx] = key
    print(max_idx+1)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    a = [int()] * (M)  # type: "List[int]"
    b = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, M, a, b)


if __name__ == '__main__':
    main()
