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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<iostream>

using namespace std;

long long tree[1 << 21];
long long lazy[1 << 21];

void akt(long long w)
{
    while(w!=0)
    {
        tree[w] = tree[w*2] + tree[w*2+1];
        w/=2;
    }

}

void dodaj(long long l, long long r, long long ll,long long rr,long long wartosc,long long w)
{
    if( l > rr || r < ll)
    {
        return;
    }

    if(l >= ll && r <=rr)
    {
        lazy[w] += wartosc;
        tree[w] += (r-l+1) * wartosc;
        akt(w/2);
        return;
    }

    lazy[2*w] += lazy[w];
    lazy[2*w+1] += lazy[w];
    tree[2*w] += (r-l+1)/2 * lazy[w];
    tree[2*w+1] += (r-l+1)/2 * lazy[w];
    lazy[w] = 0;

   long long sr = (l+r)/2;
    dodaj(l,sr,ll,rr,wartosc,w*2);
    dodaj(sr+1,r,ll,rr,wartosc,w*2+1);


}

long long czytajsume(long long l, long long r, long long ll, long long rr, long long w)
{
    long long suma = 0;

    if( l > rr || r < ll)
    {
        return 0;
    }

    if(l >= ll && r <=rr)
    {
        return tree[w];
    }

    lazy[2*w] += lazy[w];
    lazy[2*w+1] += lazy[w];
    tree[2*w] += (r-l+1)/2 * lazy[w];
    tree[2*w+1] += (r-l+1)/2 * lazy[w];
    lazy[w] = 0;

    long long sr = (l+r)/2;
    suma += czytajsume(l,sr,ll,rr,w*2);
    suma += czytajsume(sr+1,r,ll,rr,w*2+1);
    return suma;


}


int main(void)
{
    std::ios_base::sync_with_stdio(false);
    cin.tie();
    cout.tie();

    long long n,z,x,y,licz1,licz2,licz3,r,wynik,a;
    cin >> n >> z;
    for(int i=0;i<z;i++)
    {
        cin>>x>>y>>a;
        if(a==1)
        {
            licz1++;
            dodaj(1,1 << 20 ,x,y,1,1);
        }
        else
            if(a==2)
            {
                licz2++;
                dodaj(1,1 << 20,x,y,1000000,1);
            }
        else
            if(a==3)
            {
                licz3++;
                dodaj(1,1 << 20,x,y,1000000000000,1);
            }
    }
    if((licz1==z)||(licz2==z)||(licz3==z))
    {
        cout<<0;
        return 0;
    }
    wynik =0;
    for(int i=1;i<=n;i++)
    {
        r=czytajsume(1,1 << 20,i,i,1);
        if((r>1000000)&&(r<1000000000000))
        {
            if(r%1000000!=0)
            {
                wynik++;
            }
        }
    }
    cout<<wynik;

    return 0;
}