#include <bits/stdc++.h>
using namespace std;

int distanceCovered(vector<vector<int>> busStops)
{
    int answer;
    answer=0;
    long long arr[1000005]={0};
    int z = busStops.size();
    for (int i = 0; i < z; i++)
    {
        arr[busStops[i][0]]++;
        arr[busStops[i][1]]--;
    }
    long long curr = 0;
    for (int i = 0; i < 1000005; i++)
    {
        curr+=arr[i];
        if (curr>0)
        {
            answer++;
        }
    }
    return answer;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int rows,cols; cin>>rows>>cols;
    vector<vector<int>> store(rows);

    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            int x; cin>>x;
            store[i].push_back(x);
        }
    }
    
    cout<<distanceCovered(store)<<'\n';
}