import std.stdio;
import std.ascii;
import std.conv;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.math;
import core.bitop;
import std.numeric;

void main()
{
    auto nm = readln.chomp.split.to!(long[]);
    long N = nm[0];
    long M = nm[1];

    long[] A = new long[](M);
    long[] B = new long[](M);

    foreach (i; 0 .. M)
    {
        auto ab = readln.chomp.split.to!(long[]);
        A[i] = ab[0];
        B[i] = ab[1];
    }

    bool[] _A = new bool[](N + 1);
    bool[] _B = new bool[](N + 1);
    _A[] = false;
    _B[] = false;
    foreach (a; A)
        _A[a] = true;
    foreach (b; B)
        _B[b] = true;

    long cnt;
    bool flag = false;
    foreach (i; 0 .. N + 1)
    {
        if (flag && _B[i])
        {
            cnt++;
            flag = false;
        }
        flag = flag || _A[i];
    }
    cnt.writeln;
}
