# https://tjkendev.github.io/procon-library/python/max_flow/dinic.html
# Dinic's algorithm
from collections import deque
class Dinic:
    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]
 
    def add_edge(self, fr, to, cap):
        forward = [to, cap, None]
        forward[2] = backward = [fr, 0, forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)
 
    def add_multi_edge(self, v1, v2, cap1, cap2):
        edge1 = [v2, cap1, None]
        edge1[2] = edge2 = [v1, cap2, edge1]
        self.G[v1].append(edge1)
        self.G[v2].append(edge2)
 
    def bfs(self, s, t):
        self.level = level = [None]*self.N
        deq = deque([s])
        level[s] = 0
        G = self.G
        while deq:
            v = deq.popleft()
            lv = level[v] + 1
            for w, cap, _ in G[v]:
                if cap and level[w] is None:
                    level[w] = lv
                    deq.append(w)
        return level[t] is not None
 
    def dfs(self, v, t, f):
        if v == t:
            return f
        level = self.level
        for e in self.it[v]:
            w, cap, rev = e
            if cap and level[v] < level[w]:
                d = self.dfs(w, t, min(f, cap))
                if d:
                    e[1] -= d
                    rev[1] += d
                    return d
        return 0
 
    def flow(self, s, t):
        flow = 0
        INF = 10**9 + 7
        G = self.G
        while self.bfs(s, t):
            *self.it, = map(iter, self.G)
            f = INF
            while f:
                f = self.dfs(s, t, INF)
                flow += f
        return flow
 

# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

n,m,*lr = map(int,read().split())

mp = iter(lr)


D = Dinic(n+3*m+8)
s = n+3*m+8-2
g = n+3*m+8-1

for i in range(n+m+2,n+2*m+3):
    D.add_edge(i, i-m-2, 1)
    D.add_edge(i, i+1, 1)
D.add_edge(n+2*m+3, n+m+1, 1)
    
for i in range(n+2*m+4,n+3*m+5):
    D.add_edge(i,i-2*m-4, 1)
    D.add_edge(i+1, i, 1)
D.add_edge(n+3*m+5, n+m+1, 1)

for i in range(n):
    D.add_edge(s,i,1)

for i in range(n+1,n+m+1):
    D.add_edge(i,g,1)

for i,(l,r) in enumerate(zip(mp,mp)):
    D.add_edge(i, n+2*m+4+l, 1)
    D.add_edge(i, n+m+2+r, 1)


ans = D.flow(s, g)

print(n-ans)


