#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';
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgZGlzdGFuY2VDb3ZlcmVkKHZlY3Rvcjx2ZWN0b3I8aW50Pj4gYnVzU3RvcHMpCnsKICAgIGludCBhbnN3ZXI7CiAgICBhbnN3ZXI9MDsKICAgIGxvbmcgbG9uZyBhcnJbMTAwMDAwNV09ezB9OwogICAgaW50IHogPSBidXNTdG9wcy5zaXplKCk7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IHo7IGkrKykKICAgIHsKICAgICAgICBhcnJbYnVzU3RvcHNbaV1bMF1dKys7CiAgICAgICAgYXJyW2J1c1N0b3BzW2ldWzFdXS0tOwogICAgfQogICAgbG9uZyBsb25nIGN1cnIgPSAwOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCAxMDAwMDA1OyBpKyspCiAgICB7CiAgICAgICAgY3Vycis9YXJyW2ldOwogICAgICAgIGlmIChjdXJyPjApCiAgICAgICAgewogICAgICAgICAgICBhbnN3ZXIrKzsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gYW5zd2VyOwp9CgppbnQgbWFpbigpCnsKICAgIGlvczo6c3luY193aXRoX3N0ZGlvKDApOwogICAgY2luLnRpZSgwKTsKCiAgICBpbnQgcm93cyxjb2xzOyBjaW4+PnJvd3M+PmNvbHM7CiAgICB2ZWN0b3I8dmVjdG9yPGludD4+IHN0b3JlKHJvd3MpOwoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgcm93czsgaSsrKQogICAgewogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgY29sczsgaisrKQogICAgICAgIHsKICAgICAgICAgICAgaW50IHg7IGNpbj4+eDsKICAgICAgICAgICAgc3RvcmVbaV0ucHVzaF9iYWNrKHgpOwogICAgICAgIH0KICAgIH0KICAgIAogICAgY291dDw8ZGlzdGFuY2VDb3ZlcmVkKHN0b3JlKTw8J1xuJzsKfQ==