// see https://gist.github.com/LumaKernel/ff55d49ee1af69b7388f15b707e75c15
const bool DEBUG = 1;
#include <iostream>
#include <vector>
#include <array>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <tuple>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <complex>
#include <random>
#include <iomanip>
#include <cassert>
#include <functional>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef tuple<ll, ll> P;
typedef tuple<ll, ll, ll> P3;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<P> VP;
typedef vector<string> VS;

#define omajinai ios::sync_with_stdio(false);cin.tie(0)

#define FOR(i,a,b) for(int i=int(a);i<int(b);++i)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(int i=int(b)-1;i>=int(a);--i)
#define RREP(i,n) RFOR(i,0,n)

#define ALL(a) (a).begin(),(a).end()
#define UNIQUE(a) (a).erase(unique(ALL(a)),(a).end())
#define EACH(i,c) REP(i,(c).size())
#define REACH(i,c) RREP(i,(c).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(ALL(c))

#define BR cout<<"\n"

#define dump(x) if(DEBUG) cerr<<"["<<__LINE__<< "] "<<#x<<"="<<(x)<<"\n"
#define dump2(x,y) if(DEBUG) cerr<<"["<<__LINE__<< "] "<<#x<<"="<<(x)\
  <<" , "<<#y<<"="<<(y)<<"\n"
#define dump3(x,y,z) if(DEBUG)cerr<<"["<<__LINE__<<"] "<<#x<<"="<<(x)\
  <<" , "<<#y<<"="<<(y)\
  <<" , "<<#z<<"="<<(z)<<"\n"

#define SAY(x) if(DEBUG) cerr<<"["<<__LINE__<< "] "<<(x)<<"\n"

#define YES(x) cout<<((x)?"YES":"NO")<<"\n"
#define Yes(x) cout<<((x)?"Yes":"No")<<"\n"
#define yes(x) cout<<((x)?"yes":"no")<<"\n"

inline int omajinai_int_in(){omajinai;int n;cin>>n;return n;}
inline ll omajinai_ll_in(){omajinai;ll n;cin>>n;return n;}
inline string omajinai_string_in(){omajinai;string n;cin>>n;return n;}
inline int int_in(){int n;cin>>n;return n;}
inline ll ll_in(){ll n;cin>>n;return n;}
inline string string_in(){string n;cin>>n;return n;}
#define oini omajinai_int_in()
#define oinl omajinai_ll_in()
#define oins omajinai_string_in()
#define ini int_in()
#define inl ll_in()
#define ins string_in()

#define isInside(x,y) (0<=(x)&&(x)<w&&0<=(y)&&(y)<h)

#define fi(x) (get<0>(x))
#define se(x) (get<1>(x))
#define th(x) (get<2>(x))
#define fo(x) (get<3>(x))

constexpr int INF = 1e9+1;
constexpr ll LINF = 1e18+1;
constexpr int MOD = 1e9+7;

template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { o << '['; EACH(i, v) o << v[i] << (i != v.size()-1 ? ", " : ""); o << "]";  return o; }

template <typename T> function<T(T,T)> constexpr mmin(){ return [](T x, T y){return x<y?x:y;}; }
template <typename T> function<T(T,T)> constexpr mmax(){ return [](T x, T y){return x>y?x:y;}; }
template <typename T> function<T(T,T)> constexpr madd(){ return [](T x, T y){return x+y;}; }
template <typename T> function<T(T,T)> constexpr modadd(){ return [](T x, T y){return ((x+y)%MOD+MOD)%MOD;}; }

ll ans;

template<typename T, int SIZE, T DEF, function<T(T,T)> &FUNC>
struct SegTree{
	T data[SIZE*4];
	int m;
	T def = DEF;
	function<T(T,T)> func = FUNC;
	void init(int n){
		m=1;
		while(m<n) m<<=1;
		REP(i, m*2-1) data[i] = def;
	}
	void update(int i, T x) {
		i += m-1;
		data[i] = x;
		while(i>0) {
			i = (i-1)>>1;
			data[i] = func(data[i*2+1], data[i*2+2]);
		}
	}
	T query(int a, int b, int k=0,int l=0, int r=-1){
		if(r<0) r=m;
		if(b<=l||r<=a) return def;
		if(a<=l&&r<=b) return data[k];
		return func(
				query(a, b, k*2+1, l, (l+r)/2),
				query(a, b, k*2+2, (l+r)/2, r)
			);
	}
	T get(int i) {
		return data[i + m - 1];
	}
	void dum(int l = -1, int r = -1) {
		if(!DEBUG) return;
		if(l<0) l = 0;
		if(r<0) r = m;
		l = max(0, l);
		r = min(m, r);
		cerr<<l<<"[";
		FOR(i, l, r) {
			cerr<<get(i)<<(i!=r-1?", ":"");
		}
		cerr<<"]"<<r<<endl;
	}
};

auto imin = mmin<int>();

const int N = 2e5+1;

using RMQ = SegTree<int, N, INF, imin>;

RMQ dp;
RMQ dp2; // dp[i][j] - j + sum(j)

P qs[N];
int b[N];
int sum(int j) {
	if(j<0) return 0;
	return b[j];
}
// inclusive
int point(int l, int r) {
	return r - l + 1 - 2*sum(r) + 2*sum(l-1);
}

int main() {
	//#####//
	int n=oini;
	dp.init(n+1);
	dp2.init(n+1);
	REP(i, n) {
		b[i+1] = ini + b[i];
	}
	int q=ini;
	REP(i, q) {
		int l=ini, r=ini;
		qs[i+1] = P(l, r);
	}
	sort(qs+1, qs+q+1);
	dp.update(0, 0);
	dp2.update(0, 0);

	FOR(i, 1, q+1) {
		int l=fi(qs[i]), r=se(qs[i]);
		int nd1 = dp.get(r);
		int nd2 = dp.query(0, l) + point(l, r);
		int nd3 = dp2.query(l, r) + r - 2*sum(r);
		int mnd = min(nd1, min(nd2, nd3));

		dp.update(r, mnd);
		dp2.update(r, mnd - r + 2*sum(r));
	}
	cout << dp.query(0, n+1) + sum(n); BR;
}

//


Measured memory_bytes: 9344