1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>

struct wierz
{ 
  int left;
  int right;
  int kolor;
};

struct wierz tree[2097153];

void init(void)
{
  int w;
  
  for(w=1048576;w<2097152;w++)
  {
     tree[w].left=1+w-1048576;
     tree[w].right=1+w-1048576;
     tree[w].kolor=0;
  }
 
  for(w=1048575;w>0;w--)
  {
     tree[w].left=tree[2*w].left;
     tree[w].right=tree[2*w+1].right;
     tree[w].kolor=0;
  }

  return;
}


void wgraj(int l,int r,int kol)
{
   int b,bp,la;
   
   la=l;	
   b=la+1048575;
   while(la<=r)
   {
      while(b>=1 && tree[b].left==la && tree[b].right<=r) {bp=b; b/=2;}
      b=bp;	
      tree[b].kolor = (tree[b].kolor | kol);
      la=tree[b].right+1;
      b=la+1048575;  
   }

   return;
}

int finisz(void)
{
   int w,L;
   
   L=0;
   for(w=1;w<1048576;w++)
   {
     tree[2*w].kolor=(tree[2*w].kolor | tree[w].kolor);
     tree[2*w+1].kolor=(tree[2*w+1].kolor | tree[w].kolor);
   }

   for(w=1048576;w<2097152;w++)
     if(tree[w].kolor==3)  L++;

   return L;
}


int main()
{
  int n,m,l,r,k;

  init();

  scanf("%d %d",&n,&m);
  while(m--)
  {
   scanf("%d %d %d",&l,&r,&k);
   if(k==3) k=4;
   wgraj(l,r,k);
  }

 printf("%d\n",finisz());
  
  return 0;
}