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
#include <iostream>                                                             
using namespace std;
int* tab = new int[1 << 21]();

int M = (1 << 20) - 1;
void co(int p, int w)
{
    if (tab[p] == 0)
        tab[p] = w;

    else if (tab[p] == 3 && w == 4)
        tab[p] += w;

    else if (tab[p] == 1 && w == 2)
        tab[p] += w;

    else if (tab[p] == 2 && w == 1)
        tab[p] += w;

    if (w == 4)
        tab[p] += 4;
    
}

void wstaw(int p, int k, int w)
{
    co(p, w);

    co(k, w);


    while (p/2 <k/2)
    {
        
        if (p % 2 == 1)
        {   if(((p/2)+1)<k/2)
            co((p / 2) + 1, w);
            p++;
        }
        else
            co(p / 2, w);

        if (k % 2 == 1)
            co(k / 2, w);
        else
        {   if (((k / 2) - 1) >p /2-1)
            co((k / 2) - 1, w);
            k--;
        }
        k /= 2;
        p /= 2;

    }

}
bool spr(int p)
{
    int ta[5] = {};
    
    while (p != 1)
    {
        if (tab[p] == 1) ta[1]++;
        if (tab[p] == 2) ta[2]++;
        if (tab[p] == 3) ta[3]++;
        if (tab[p] > 3) ta[4]++;
        p /= 2;

    }
    if (ta[4] > 0) return false;
    else if (ta[3] > 0) return true;
    else if (ta[1] > 0 && ta[2] > 0) return true;
    else return false;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int puszek, t,sum=0;
    cin >> puszek >> t;

    for (int i = 0; i < t; i++)
    {
        int p, k, farba;
        cin >> p >> k >> farba;
        if (farba == 3)
            wstaw(M + p, M + k, 4);

        else
            wstaw(M + p, M + k, farba);

    }

    for (int i = 1; i <= puszek; i++)
    {
        if (spr(M + i))
          sum++;
        

    }

    cout << sum;




    return 0;
}