include<bits/stdc++.h>

using namespace std;

define lowbit(x) ((x)&(-x))

define pi acos(-1.0)

define eps 1e-8

define MOD 1000000007

define INF 1000000000

define mem(a,b) memset(a,b,sizeof(a))

define FOR(i,a,n) for(register int i=a; i<=n; ++i)

define FDR(i,a,n) for(register int i=a; i>=n; --i)

define bug puts("H");

define lch p<<1,l,mid

define rch p<<1|1,mid+1,r

define mp make_pair

define pb push_back

typedef pair<int,int> PII;
typedef vector<int> VI;</int>

pragma comment(linker, "/STACK:1024000000,1024000000")

typedef long long LL;
inline int Scan() {
int x=0;int f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){x=x10+ch-'0'; ch=getchar();}
return x
f;
}
const int N=2000005;
//Code beg....

const int M=1000005;
struct Node{int be,en,c;}aa[N];
int beg[N],ed[N],sum[N];
bool cover[N];
void build(int n,int be,int en)
{
beg[n]=be,ed[n]=en;
if(be==en)
{
sum[n]=1;
return;
}
int mid=(be+en)>>1;
build(n<<1,be,mid);
build((n<<1)+1,mid+1,en);
sum[n]=sum[n<<1]+sum[(n<<1)+1];
}
int get(int n,int be,int en)
{
if(cover[n])
return 0;
if(beg[n]==be&&ed[n]==en)
return sum[n];
int mid=(beg[n]+ed[n])>>1;
if(en<=mid) return get(n<<1,be,en);
else if(be>mid) return get((n<<1)+1,be,en);
else return get(n<<1,be,mid)+get((n<<1)+1,mid+1,en);
}
void change1(int n,int be,int en)
{
if(be==beg[n]&&en==ed[n]) {
cover[n]=true,sum[n]=0;
return ;
}
int mid=(beg[n]+ed[n])>>1;
if(en<=mid) change1(n<<1,be,en);
else if(be>mid) change1((n<<1)+1,be,en);
else change1(n<<1,be,mid),change1((n<<1)+1,mid+1,en);
sum[n]=sum[n<<1]+sum[(n<<1)+1];
}
void change(int n,int be,int en,int s)
{
if(beg[n]==be&&ed[n]==en&&s==sum[n]) {
cover[n]=true,sum[n]=0;
return ;
}
int mid=(beg[n]+ed[n])>>1,ss;
if(en<=mid) change(n<<1,be,en,s);
else if(be>mid) change((n<<1)+1,be,en,s);
else {
ss=get(n,mid+1,en);
if(ss>=s) change((n<<1)+1,mid+1,en,s);
else change1((n<<1)+1,mid+1,en),change(n<<1,be,mid,s-ss);
}
sum[n]=sum[n<<1]+sum[(n<<1)+1];
}
int cmp(const void *a,const void *b)
{
return ((Node *)a)->en-((Node *)b)->en;
}
int main()
{
int n,i,j,t,m;
build(1,0,500000);
scanf("%d%d",&n,&m);
FOR(i,0,m-1) scanf("%d%d%d",&aa[i].be,&aa[i].en,&aa[i].c);
qsort(aa,m,sizeof(Node),cmp);
FOR(i,0,m-1) {
t=get(1,aa[i].be,aa[i].en);
t=aa[i].en-aa[i].be+1-t;
if(t<aa[i].c) change(1,aa[i].be,aa[i].en,aa[i].c-t);
}
printf("%d\n",500001-get(1,0,500000));
}