#define _CRT_SECURE_NO_DEPRECATE
#define _SECURE_SCL 0

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <iostream>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <cctype>
#include <sstream>
#include <cassert>
#include <bitset>
#include <memory.h>
#include <complex>

using namespace std;

#pragma comment(linker, "/STACK:200000000")

typedef long long int64;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define fore(i, a, n) for(int i = (int)(a); i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) (int(a.size()) - 1)
#define all(a) a.begin(), a.end()

const double EPS = 1E-9;
const int INF = 1000000000;
const int64 INF64 = (int64) 1E18;
const double PI = 3.1415926535897932384626433832795;

const int dd[2] = {+1, -1};
int p0[2100][2100], p1[2100][2100];
int n, m, ax, ay;

void ans(int x, int y) {
    p0[x][y] = 1;
    p1[x][y] = 1;
    printf("%d %d\n", x + 1, y + 1);
}

int get0(int x, int y) {
    if (x < 0 || y < 0 || x >= n || y >= m || !p0[x][y])
        return 0;
    return p0[x][y] += get0(x + p0[x][y], y + p0[x][y]);
}

int get1(int x, int y) {
    if (x < 0 || y < 0 || x >= n || y >= m || !p1[x][y])
        return 0;
    return p1[x][y] += get1(x + p1[x][y], y - p1[x][y]);
}

inline bool isin(int x, int y) {
    return 0 <= x && x < n && 0 <= y && y < m;
}

void query(int x, int y, int b, int tp) {
    if (x < 0) {
        y += dd[tp] * -x;
        x = 0;
    }
    if (y < 0) {
        if (tp == 1)
            return;
        x += -y;
        y = 0;
    }
    if (y >= m) {
        if (tp == 0)
            return;
        x += y - m + 1;
        y = m - 1;
    }

    if (x >= n)
        return;

    int val = (tp ? get1 : get0)(x, y);
    int rx = x + val;
    int ry = y + (!tp ? val : -val);
    if (rx > b || !isin(rx, ry))
        return;

    if (rx < ax || rx == ax && ry < ay) {
        ax = rx;
        ay = ry;
    }
}

void solve(int x, int y) {
    ax = ay = INF;
    for (int d = 0; ; d++) {
        query(x - d, y, x, 0);
        query(x - d, y, x, 1);

        if (ax != INF)
            return ans(ax, ay);

        query(x, y - d, min(n - 1, x + d), 0);
        query(x, y + d, min(n - 1, x + d), 1);

        if (ax != INF)
            return ans(ax, ay);
    }
}

int main() {
#ifdef RADs_project
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

    int k;
    cin >> n >> m >> k;

    forn(i, k) {
        int x, y;

        scanf("%d%d", &x, &y);
        x--;
        y--;

        solve(x, y);
    }

    cerr << clock() << endl;

    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _SECURE_SCL 0

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <iostream>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <cctype>
#include <sstream>
#include <cassert>
#include <bitset>
#include <memory.h>
#include <complex>

using namespace std;

#pragma comment(linker, "/STACK:200000000")

typedef long long int64;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define fore(i, a, n) for(int i = (int)(a); i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) (int(a.size()) - 1)
#define all(a) a.begin(), a.end()

const double EPS = 1E-9;
const int INF = 1000000000;
const int64 INF64 = (int64) 1E18;
const double PI = 3.1415926535897932384626433832795;

const int dd[2] = {+1, -1};
int p[2][2100][2100], n, m, ax, ay;

void ans(int x, int y) {
    p[0][x][y] = 1;
    p[1][x][y] = 1;
    printf("%d %d\n", x + 1, y + 1);
}

int get(int tp, int x, int y) {
    if (x < 0 || y < 0 || x >= n || y >= m || p[tp][x][y] == 0)
        return 0;

    return p[tp][x][y] = p[tp][x][y] +
        get(tp, x + p[tp][x][y], y + (!tp ? p[tp][x][y] : -p[tp][x][y]));
}

inline bool isin(int x, int y) {
    return 0 <= x && x < n && 0 <= y && y < m;
}

void query(int x, int y, int b, int tp) {
    if (x < 0) {
        y += dd[tp] * -x;
        x = 0;
    }
    if (y < 0) {
        if (tp == 1)
            return;
        x += -y;
        y = 0;
    }
    if (y >= m) {
        if (tp == 0)
            return;
        x += y - m + 1;
        y = m - 1;
    }

    if (x >= n)
        return;

    int val = get(tp, x, y);
    int rx = x + val;
    int ry = y + (!tp ? val : -val);
    if (rx > b || !isin(rx, ry))
        return;

    if (rx < ax || rx == ax && ry < ay) {
        ax = rx;
        ay = ry;
    }
}

void solve(int x, int y) {
    ax = ay = INF;
    for (int d = 0; ; d++) {
        query(x - d, y, x, 0);
        query(x - d, y, x, 1);

        if (ax != INF)
            return ans(ax, ay);

        query(x, y - d, min(n - 1, x + d), 0);
        query(x, y + d, min(n - 1, x + d), 1);

        if (ax != INF)
            return ans(ax, ay);
    }
}

int main() {
#ifdef RADs_project
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

    int k;
    cin >> n >> m >> k;
/*
    n = m = 2000;
    k = 100000;
*/
    forn(i, n)
        forn(j, m)
            p[0][i][j] = p[1][i][j] = 0;

    forn(i, k) {
        int x, y;

        scanf("%d%d", &x, &y);
        x--;
        y--;

//      x = y = 1999;

        solve(x, y);
    }

    cerr << clock() << endl;

    return 0;
}


#include <algorithm>
#include <cstdio>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
int n,m,k,x,y,a,b,d[2010][2010],v[2010][2010];
bool solve(int x,int y,int k) {
	int l=max(1,x-k),r=min(x+k,n),t;
	rep(i,l,r+1) {
		t=k-abs(i-x);
		if (y-t>0&&!v[i][y-t]) return a=i,b=y-t,1;
		if (y+t<=m&&!v[i][y+t]) return a=i,b=y+t,1;
	}
	return 0;
}
int main() {
	scanf("%d%d%d",&n,&m,&k);
	rep(t,0,k) {
		scanf("%d%d",&x,&y);
		rep(i,-2,3) rep(j,-2,3) {
			if (x+i<1||x+i>n||y+j<1||y+j>m) continue;
			d[x][y]=max(d[x][y],d[x+i][y+j]-abs(i)-abs(j));
		}
		while (!solve(x,y,d[x][y])) d[x][y]++;
		printf("%d %d\n",a,b);
		v[a][b]=1;
	}
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <math.h>
using namespace std;

struct NODE
{
    int x, y;
    void update(int X, int Y, int _x, int _y)
    {
        if (abs(X-_x)+abs(Y-_y)>abs(X-x)+abs(Y-y)) return;
        if (abs(X-_x)+abs(Y-_y)<abs(X-x)+abs(Y-y))
        {
            x = _x, y = _y;
            return;
        }
        if (_x>x) return;
        if (_x<x)
        {
            x = _x, y = _y;
            return;
        }
        y = min(y, _y);
    }
};

const int maxn = 2007, maxnum = 100000000;
      int n, m, k, k_sqrt;
      int L[maxn][maxn], R[maxn][maxn];

NODE count(int x, int y)
{
    NODE p;
    p.x = p.y = maxnum;
    for (int i = 0; i < n; i++)
    {
if (x-i <= 0 && x+i > n) break;
if (i > abs(p.x-x)+abs(p.y-y)) break;
        if (x-i >= 1)
{
        int l = L[x-i][y], r = R[x-i][y];
        if (l>=1) p.update(x, y, x-i, l);
        if (r<=m) p.update(x, y, x-i, r);
}
        if (i && x+i <= n)
{
        int l = L[x+i][y], r = R[x+i][y];
        if (l>=1) p.update(x, y, x+i, l);
        if (r<=m) p.update(x, y, x+i, r);
}
    }
    return p;
}

void insert(int x, int y)
{
    int l = L[x][y-1], r = R[x][y+1];
    for (int j = l; j <= r; j++)
    {
        if (L[x][j]==y) L[x][j] = l;
        if (R[x][j]==y) R[x][j] = r;
    }
}

void work()
{
bool sign = false;
if (n > m)
{
swap(n, m);
sign = true;
}
    for (int i = 1; i <= n; i++)
    {
        L[i][0] = 0, R[i][m+1] = m+1;
        for (int j = 1; j <= m; j++) L[i][j] = R[i][j] = j;
    }
    for (int i = 1; i <= k; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
if (sign) swap(x, y);
        NODE p = count(x, y);
        insert(p.x, p.y);
if (sign) swap(p.x, p.y);
        printf("%d %d\n", p.x, p.y);
    }
}

int main()
{
    while (scanf("%d%d%d", &n, &m, &k)!=EOF) work();
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <math.h>
using namespace std;

struct NODE
{
    int x, y;
    void update(int X, int Y, int _x, int _y)
    {
        if (abs(X-_x)+abs(Y-_y)>abs(X-x)+abs(Y-y)) return;
        if (abs(X-_x)+abs(Y-_y)<abs(X-x)+abs(Y-y))
        {
            x = _x, y = _y;
            return;
        }
        if (_x>x) return;
        if (_x<x)
        {
            x = _x, y = _y;
            return;
        }
        y = min(y, _y);
    }
};

const int maxn = 2007, maxnum = 100000000;
      int n, m, k, k_sqrt;
      int L[maxn][maxn], R[maxn][maxn];

NODE count(int x, int y)
{
    int Up = max(1, x-k_sqrt), Down = min(n, x+k_sqrt);
    NODE p;
    p.x = p.y = maxnum;
    for (int i = 0; i < n; i++)
    {
if (i >= abs(p.x-x)+abs(p.y-y)) break;
        if (x-i >= 1)
{
        int l = L[x-i][y], r = R[x-i][y];
        if (l>=1) p.update(x-i, y, i, l);
        if (r<=m) p.update(x-i, y, i, r);
}
        if (i && x+i <= n)
{
        int l = L[x-i][y], r = R[x-i][y];
        if (l>=1) p.update(x+i, y, i, l);
        if (r<=m) p.update(x+i, y, i, r);
}
    }
    return p;
}

void insert(int x, int y)
{
    int l = L[x][y-1], r = R[x][y+1];
    for (int j = l; j <= r; j++)
    {
        if (L[x][j]==y) L[x][j] = l;
        if (R[x][j]==y) R[x][j] = r;
    }
}

void work()
{
    //k_sqrt = (int)sqrt(2*k)+1;
    k_sqrt = max(k_sqrt, k/m);
    for (int i = 1; i <= n; i++)
    {
        L[i][0] = 0, R[i][m+1] = m+1;
        for (int j = 1; j <= m; j++) L[i][j] = R[i][j] = j;
    }
    for (int i = 1; i <= k; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        NODE p = count(x, y);
        insert(p.x, p.y);
        printf("%d %d\n", p.x, p.y);
    }
}

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    while (scanf("%d%d%d", &n, &m, &k)!=EOF) work();
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    long requests, time, i, j, server_id[100];
    short servers, duration, server_req, help = 0, sum = 0, result[100000];

    scanf("%hu",&servers);
    scanf("%li",&requests);

    short available[100000];
    for(i = 0; i < 100000; i++){
        available[i] = 0;
    }
    for(i = 0; i < 100; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%li",&time);
        scanf("%hu",&server_req);
        scanf("%hu",&duration);

        for(j = time; j < time + duration; j++){
            if(j < 100000){
                        available[j] += server_req;
                    }

        }

        if(available[time] <= servers){
           for(j = 0; help < server_req; j++){
                if(server_id[j] <= time){
                    sum += j + 1;
                    server_id[j] = time + duration;
                    help++;
                }
           }

           result[i] = sum;
           help = 0;
           sum = 0;
        }
        else{

            result[i] = 0;
            for(j = time; j < time + duration; j++){
                    if(j < 100000){
                        available[j] -= server_req;
                    }

        }

        }

    }
    for(i = 0; i < requests; i++){
        if(result[i] == 0){
            printf("-1 \n");
        }
        else{
            printf("%hu \n",result[i]);
        }

    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    long requests, time, i, j, server_id[100];
    short servers, duration, server_req, help = 0, sum = 0, result[100000];

    scanf("%hu",&servers);
    scanf("%li",&requests);

    short available[100000];
    for(i = 0; i < 100000; i++){
        available[i] = 0;
    }
    for(i = 0; i < 100; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%li",&time);
        scanf("%hu",&server_req);
        scanf("%hu",&duration);

        available[time] += server_req;

        if(available[time] <= servers){
            for(j = time + 1; j < time + duration; j++){
            if(j < 100000){
                        available[j] += server_req;
                    }
        }

           for(j = 0; help < server_req; j++){
                if(server_id[j] <= time){
                    sum += j + 1;
                    server_id[j] = time + duration;
                    help++;
                }
           }

           result[i] = sum;
           help = 0;
           sum = 0;
        }
        else{

            result[i] = 0;

        }

    }
    for(i = 0; i < requests; i++){
        if(result[i] == 0){
            printf("-1 \n");
        }
        else{
            printf("%hu \n",result[i]);
        }

    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    long requests, time, i, j, server_id[100];
    short servers, duration, server_req, help = 0, sum = 0, result[100000];

    scanf("%hu",&servers);
    scanf("%li",&requests);

    short available[100000];
   /* for(i = 0; i < 100000; i++){
        available[i] = 0;
    }*/
    for(i = 0; i < 100; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%li",&time);
        scanf("%hu",&server_req);
        scanf("%hu",&duration);

        available[time] += server_req;

        if(available[time] <= servers){
           /* for(j = time + 1; j < time + duration; j++){
            if(j < 100000){
                        available[j] += server_req;
                    }
        }*/

           for(j = 0; help < server_req; j++){
                if(server_id[j] <= time){
                    sum += j + 1;
                    server_id[j] = time + duration;
                    help++;
                }
           }

           result[i] = sum;
           help = 0;
           sum = 0;
        }
        else{
            result[i] = 0;
        }

    }
    for(i = 0; i < requests; i++){
        if(result[i] == 0){
            printf("-1 \n");
        }
        else{
            printf("%hu \n",result[i]);
        }

    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{

    long requests, time[100000], i, j, server_id[100];
    short available[100000], servers, server_req[100000], help = 0, sum = 0, duration[100000];

    scanf("%hu",&servers);
    scanf("%li",&requests);

    for(i = 0; i < servers; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%li",&time[i]);
        scanf("%hu",&server_req[i]);
        scanf("%hu",&duration[i]);
        available[i] = 0;
    }

    for(i = 0; i < requests; i++){

        for(j = i; time[i] + duration[i] > time[j]; j++){
            available[j] += server_req[i];
        }

        if(available[i] <= servers){

           for(j = 0; help < server_req[i]; j++){

                if(server_id[j] <= time[i]){
                    sum += j + 1;
                    server_id[j] = time[i] + duration[i];
                    help++;
                }
           }

           printf("%hu \n",sum);
           help = 0;
           sum = 0;
        }
        else{
            printf("-1 \n");
        }

    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    long requests, time[100000], i, j, server_id[100];
    short servers, duration[100000], server_req[100000], help = 0, sum = 0;

    scanf("%hu",&servers);
    scanf("%li",&requests);

    short available[100000];
    for(i = 0; i < 100000; i++){
        available[i] = 0;
    }
    for(i = 0; i < 100; i++){
        server_id[i] = 0;
    }
    for(i = 0; i < requests; i++){
        scanf("%li",&time[i]);
        scanf("%hu",&server_req[i]);
        scanf("%hu",&duration[i]);
    }

    for(i = 0; i < requests; i++){
        for(j = time[i]; j < time[i] + duration[i]; j++){
            available[j] += server_req[i];

        }

        if(available[time[i]] <= servers){
           for(j = 0; help < server_req[i]; j++){
                if(server_id[j] <= time[i]){
                    sum += j + 1;
                    server_id[j] = time[i] + duration[i];
                    help++;
                }
           }
           printf("%hu \n",sum);
           help = 0;
           sum = 0;
        }
        else{
            printf("-1 \n");

            for(j = time[i]; j < time[i] + duration[i]; j++){
            available[j] -= server_req[i];
        }

        }

    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int requests, time, i, j, server_id[100];
    int servers, duration, server_req, help = 0, sum = 0, result[100000];

    scanf("%d",&servers);
    scanf("%d",&requests);

    int available[100000];
   /* for(i = 0; i < 100000; i++){
        available[i] = 0;
    }*/
    for(i = 0; i < 100; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%d",&time);
        scanf("%d",&server_req);
        scanf("%d",&duration);

        available[time] += server_req;

        if(available[time] <= servers){
            for(j = time + 1; j < time + duration; j++){
            /*if(j < 100000){*/
                        available[j] += server_req;
                    //}
        }

           for(j = 0; help < server_req; j++){
                if(server_id[j] <= time){
                    sum += j + 1;
                    server_id[j] = time + duration;
                    help++;
                }
           }

           result[i] = sum;
           help = 0;
           sum = 0;
        }
        else{
            result[i] = -1;
        }

    }
    for(i = 0; i < requests; i++){
        if(result[i] == 0){
            printf("-1 \n");
        }
        else{
            printf("%d \n",result[i]);
        }

    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{

    long requests, time[100000], i, j, server_id[100];
    short available[100000], servers, server_req[100000], help = 0, sum = 0, duration[100000];

    scanf("%hu",&servers);
    scanf("%li",&requests);

    for(i = 0; i < servers; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%li",&time[i]);
        scanf("%hu",&server_req[i]);
        scanf("%hu",&duration[i]);
        available[i] = 0;
    }

    for(i = 0; i < requests; i++){

        for(j = i; time[i] + duration[i] > time[j]; j++){
            available[j] += server_req[i];
        }

        if(available[i] <= servers){

           for(j = 0; help < server_req[i]; j++){

                if(server_id[j] <= time[i]){
                    sum += j + 1;
                    server_id[j] = time[i] + duration[i];
                    help++;
                }
           }

           printf("%hu \n",sum);
           help = 0;
           sum = 0;
        }
        else{
            for(j = i; time[i] + duration[i] > time[j]; j++){
            available[j] -= server_req[i];
        }

            printf("-1 \n");
        }

    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    long requests, time, i, j;
    short servers, duration, server_req, help = 0, sum = 0, result[100000], server_id[100];

    scanf("%hu",&servers);
    scanf("%li",&requests);

    short available[100000];
   /* for(i = 0; i < 100000; i++){
        available[i] = 0;
    }*/
    for(i = 0; i < 100; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%li",&time);
        scanf("%hu",&server_req);
        scanf("%hu",&duration);

        available[time] += server_req;

        if(available[time] <= servers){
            for(j = time + 1; j < time + duration; j++){
            if(j < 100000){
                        available[j] += server_req;
                    }
        }

           for(j = 0; help < server_req; j++){
                if(server_id[j] <= time){
                    sum += j + 1;
                    server_id[j] = time + duration;
                    help++;
                }
           }

           result[i] = sum;
           help = 0;
           sum = 0;
        }
        else{
            result[i] = 0;
        }

    }
    for(i = 0; i < requests; i++){
        if(result[i] == 0){
            printf("-1 \n");
        }
        else{
            printf("%hu \n",result[i]);
        }

    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    long requests, time, i, j, server_id[100];
    short servers, duration, server_req, help = 0, sum = 0, result[100000];

    scanf("%hu",&servers);
    scanf("%li",&requests);

    short available[100000];
   /* for(i = 0; i < 100000; i++){
        available[i] = 0;
    }*/
    for(i = 0; i < 100; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%li",&time);
        scanf("%hu",&server_req);
        scanf("%hu",&duration);

        available[time] += server_req;

        if(available[time] <= servers){
            for(j = time + 1; j < time + duration; j++){
            if(j < 100000){
                        available[j] += server_req;
                    }
        }

           for(j = 0; help < server_req; j++){
                if(server_id[j] <= time){
                    sum += j + 1;
                    server_id[j] = time + duration;
                    help++;
                }
           }

           result[i] = sum;
           help = 0;
           sum = 0;
        }
        else{
            result[i] = 0;
        }

    }
    for(i = 0; i < requests; i++){
        if(result[i] == 0){
            printf("-1 \n");
        }
        else{
            printf("%hu \n",result[i]);
        }

    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int requests, time, i, j, server_id[100];
    int servers, duration, server_req, help = 0, sum = 0, result[100000];

    scanf("%d",&servers);
    scanf("%d",&requests);

    int available[100000];
   /* for(i = 0; i < 100000; i++){
        available[i] = 0;
    }*/
    for(i = 0; i < 100; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%d",&time);
        scanf("%d",&server_req);
        scanf("%d",&duration);

        available[time] += server_req;

        if(available[time] <= servers){
            for(j = time + 1; j < time + duration; j++){
            if(j < 100000){
                        available[j] += server_req;
                    }
        }

           for(j = 0; help < server_req; j++){

                if(server_id[j] <= time){
                    sum += j + 1;
                    server_id[j] = time + duration;
                    help++;
                }
           }

           result[i] = sum;
           help = 0;
           sum = 0;
        }
        else{
            result[i] = -1;
        }

    }
    for(i = 0; i < requests; i++){

        printf("%d \n",result[i]);

    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

#define N 100
#define Q 100000
int main()
{
    int n,q;
    int server[N]; // � ������ ��� ��������������� ���� server
    int output [Q]; // �� ���������� ��� ���� task
    int i,j,t,k,d,sum;
    int free; // ����� servers ���� ���������� ��� ���� task

    scanf ("%d %d",&n,&q);
    for (i=0;i<n;i++)
        server[i]=1; // ������ ���� �� servers ����� ���������� ��� �� ������ ������ 1

    for (j=0;j<q;j++) // ��� ���� task
    {
        scanf("%d %d %d",&t, &k, &d);

        sum = 0;

        free=0;
        for (i=0;(free<k) && (i<n);i++) // ������ ���� ������� ������������� ��� servers
            if (server[i]<=t)
                free++;      // ��� ������ ���� ����������� servers

        if (free>=k)   // �� �������� ����������� k ���������� servers �� task ������ �� �������������
        {
            free=0;
            for (i=0;(free<k) && (i<n);i++)  //������ ���� ���� servers
            {
                if (server[i]<=t)  //��� ���� ���������
                {
                    free++;
                    server[i] = t+d;  //��� �������� �� ����� �������������
                    sum+=i+1;   // ��� �������� ��� ������ ��� ��� sum
                }
            }
            output[j]=sum; // � ������ ����� �� sum
        }
        else
            output[j]=-1; // �� ��� ����� �� ������� �� task, � ������ ����� -1
    }
    for (j=0;j<q;j++)
        printf ("%d\n",output[j]);  // �������� ��� ������� ��� ��� �� tasks
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{

    int requests, time[100000], i, j, server_id[100];
    int servers, duration[100000], server_req[100000], help = 0, sum = 0;
    int available[100000];

    scanf("%d",&servers);
    scanf("%d",&requests);

    for(i = 0; i < servers; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%d",&time[i]);
        scanf("%d",&server_req[i]);
        scanf("%d",&duration[i]);
        available[i] = 0;
    }

    for(i = 0; i < requests; i++){

        for(j = i; time[i] + duration[i] > time[j]; j++){
            available[j] += server_req[i];
        }

        if(available[i] <= servers){

           for(j = 0; help < server_req[i]; j++){

                if(server_id[j] <= time[i]){
                    sum += j + 1;
                    server_id[j] = time[i] + duration[i];
                    help++;
                }
           }

           printf("%d \n",sum);
           help = 0;
           sum = 0;
        }
        else{
            printf("-1 \n");
        }

    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    long requests, time[100000], i, j;
    short servers, duration[100000], server_req[100000];

    scanf("%hu",&servers);
    scanf("%li",&requests);

    short available[100000];
    for(i = 0; i < 100000; i++){
        available[i] = 0;
    }
    for(i = 0; i < requests; i++){
        scanf("%li",&time[i]);
        scanf("%hu",&server_req[i]);
        scanf("%hu",&duration[i]);
    }

    for(i = 0; i < requests; i++){
        for(j = time[i]; j < time[i] + duration[i]; j++){
            available[j] += server_req[i];

        }

        if(available[time[i]] <= servers){
            printf("%li \n",time[i] + server_req[i] + duration[i]);
        }
        else{
            printf("-1 \n");
        }

    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int requests, time, i, j, server_id[100];
    int servers, duration, server_req, help = 0, sum = 0, result[100000], timer = 0;

    scanf("%d",&servers);
    scanf("%d",&requests);

    int available[100000];
   /* for(i = 0; i < 100000; i++){
        available[i] = 0;
    }*/
    for(i = 0; i < 100; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%d",&time);
        scanf("%d",&server_req);
        scanf("%d",&duration);

        available[time] += server_req;

        timer++;

        if(available[time] <= servers){
            for(j = time + 1; j < time + duration; j++){
            if(j < 100000){timer++;
                        available[j] += server_req;
                    }
        }

           for(j = 0; help < server_req; j++){
                timer++;
                if(server_id[j] <= time){
                    sum += j + 1;
                    server_id[j] = time + duration;
                    help++;
                }
           }

           result[i] = sum;
           help = 0;
           sum = 0;
        }
        else{
            result[i] = -1;
        }

    }
    for(i = 0; i < requests; i++){
        timer++;
        printf("%d \n",result[i]);

    }
    printf("%d timer",timer);
    return 0;
}

/*#include <stdio.h>
#include <stdlib.h>

#define N 100
#define Q 100000
int main()
{
    int n,q;
    int server[N]; // � ������ ��� ��������������� ���� server
    int output [Q]; // �� ���������� ��� ���� task
    int i,j,t,k,d,sum;
    int free; // ����� servers ���� ���������� ��� ���� task
    int timer = 0;

    scanf ("%d %d",&n,&q);
    for (i=0;i<n;i++)
        server[i]=1; // ������ ���� �� servers ����� ���������� ��� �� ������ ������ 1

    for (j=0;j<q;j++) // ��� ���� task
    {
        timer++;
        scanf("%d %d %d",&t, &k, &d);

        sum = 0;

        free=0;
        for (i=0;(free<k) && (i<n);i++){
                timer++;// ������ ���� ������� ������������� ��� servers
            if (server[i]<=t)
                free++;      // ��� ������ ���� ����������� servers
        }
        if (free>=k)   // �� �������� ����������� k ���������� servers �� task ������ �� �������������
        {
            free=0;
            for (i=0;(free<k) && (i<n);i++)  //������ ���� ���� servers
            {
                timer++;
                if (server[i]<=t)  //��� ���� ���������
                {
                    free++;
                    server[i] = t+d;  //��� �������� �� ����� �������������
                    sum+=i+1;   // ��� �������� ��� ������ ��� ��� sum
                }
            }
            output[j]=sum; // � ������ ����� �� sum
        }
        else
            output[j]=-1; // �� ��� ����� �� ������� �� task, � ������ ����� -1
    }
    for (j=0;j<q;j++){timer++;
        printf ("%d\n",output[j]);}
        printf("%d the timer = ",timer);  // �������� ��� ������� ��� ��� �� tasks
    return 0;
}
*/


#include <stdio.h>
#include <stdlib.h>

int main()
{
    long requests, time/*[100000]*/, i, j, server_id[100];
    short servers, duration/*[100000]*/, server_req/*[100000]*/, help = 0, sum = 0, result[100000];

    scanf("%hu",&servers);
    scanf("%li",&requests);

    short available[100000];
    for(i = 0; i < 100000; i++){
        available[i] = 0;
    }
    for(i = 0; i < 100; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%li",&time);
        scanf("%hu",&server_req);
        scanf("%hu",&duration);

        for(j = time; j < time + duration; j++){
            available[j] += server_req;

        }

        if(available[time] <= servers){
           for(j = 0; help < server_req; j++){
                if(server_id[j] <= time){
                    sum += j + 1;
                    server_id[j] = time + duration;
                    help++;
                }
           }

           result[i] = sum;
           help = 0;
           sum = 0;
        }
        else{

            result[i] = 0;
            for(j = time; j < time + duration; j++){
            available[j] -= server_req;
        }

        }

    }
    for(i = 0; i < requests; i++){
        if(result[i] == 0){
            printf("-1 \n");
        }
        else{
            printf("%hu \n",result[i]);
        }

    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{

    long requests, time[100000], i, j, server_id[100];
    long servers, duration[100000], server_req[100000], help = 0, sum = 0;
    long available[100000];

    scanf("%li",&servers);
    scanf("%li",&requests);

    for(i = 0; i < servers; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%li",&time[i]);
        scanf("%li",&server_req[i]);
        scanf("%li",&duration[i]);
        available[i] = 0;
    }

    for(i = 0; i < requests; i++){

        for(j = i; time[i] + duration[i] > time[j]; j++){
            available[j] += server_req[i];
        }

        if(available[i] <= servers){

           for(j = 0; help < server_req[i]; j++){

                if(server_id[j] <= time[i]){
                    sum += j + 1;
                    server_id[j] = time[i] + duration[i];
                    help++;
                }
           }

           printf("%li \n",sum);
           help = 0;
           sum = 0;
        }
        else{
            printf("-1 \n");
        }

    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{

    int requests, time, i, j, server_id[100];
    int servers, duration, server_req, help = 0, sum = 0;
    long available[100000];

    scanf("%d",&servers);
    scanf("%d",&requests);

   /* for(i = 0; i < 100000; i++){
        available[i] = 0;
    }*/
    for(i = 0; i < servers; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%d",&time);
        scanf("%d",&server_req);
        scanf("%d",&duration);

        available[time] += server_req;

        if(available[time] <= servers){
            for(j = time + 1; j < time + duration; j++){
                available[j] += server_req;

        }

           for(j = 0; help < server_req; j++){

                if(server_id[j] <= time){
                    sum += j + 1;
                    server_id[j] = time + duration;
                    help++;
                }
           }

           available[i] = sum;
           help = 0;
           sum = 0;
        }
        else{
            available[i] = -1;
        }

    }
    for(i = 0; i < requests; i++){

        printf("%d \n",available[i]);

    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int requests, time, i, j, server_id[100];
    int servers, duration, server_req, help = 0, sum = 0, result[100000];

    scanf("%d",&servers);
    scanf("%d",&requests);

    int available[100000];
   /* for(i = 0; i < 100000; i++){
        available[i] = 0;
    }*/
    for(i = 0; i < 100; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%d",&time);
        scanf("%d",&server_req);
        scanf("%d",&duration);

        available[time] += server_req;

        if(available[time] <= servers){
            for(j = time + 1; j < time + duration; j++){
                available[j] += server_req;

        }

           for(j = 0; help < server_req; j++){

                if(server_id[j] <= time){
                    sum += j + 1;
                    server_id[j] = time + duration;
                    help++;
                }
           }

           result[i] = sum;
           help = 0;
           sum = 0;
        }
        else{
            result[i] = -1;
        }

    }
    for(i = 0; i < requests; i++){

        printf("%d \n",result[i]);

    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{

    long requests, time[100000], i, j, server_id[100];
    short available[100000], servers, server_req[100000], help = 0, sum = 0, duration[100000];

    scanf("%hu",&servers);
    scanf("%li",&requests);

    for(i = 0; i < servers; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%li",&time[i]);
        scanf("%hu",&server_req[i]);
        scanf("%hu",&duration[i]);
        available[i] = 0;
    }

    for(i = 0; i < requests; i++){

        for(j = i; time[i] + duration[i] > time[j]; j++){
            available[j] += server_req[i];
        }

        if(available[i] <= servers){

           for(j = 0; help < server_req[i]; j++){

                if(server_id[j] <= time[i]){
                    sum += j + 1;
                    server_id[j] = time[i] + duration[i];
                    help++;
                }
           }

           printf("%hu \n",sum);
           help = 0;
           sum = 0;
        }
        else{
            for(j = i; time[i] + duration[i] > time[j]; j++){
            available[j] += server_req[i];
        }

            printf("-1 \n");
        }

    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    long requests, time, i, j, server_id[100];
    short servers, duration, server_req, help = 0, sum = 0, result[100000];

    scanf("%hu",&servers);
    scanf("%li",&requests);

    short available[100000];
    for(i = 0; i < 100000; i++){
        available[i] = 0;
    }
    for(i = 0; i < 100; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%li",&time);
        scanf("%hu",&server_req);
        scanf("%hu",&duration);

        for(j = time; j < time + duration; j++){
            if(j < 100000){
                        available[j] += server_req;
                    }

        }

        if(available[time] <= servers){
           for(j = 0; help < server_req; j++){
                if(server_id[j] <= time){
                    sum += j + 1;
                    server_id[j] = time + duration;
                    help++;
                }
           }

           result[i] = sum;
           help = 0;
           sum = 0;
        }
        else{

            result[i] = 0;
            for(j = time; j < time + duration; j++){
                    available[j] -= server_req;

        }

        }

    }
    for(i = 0; i < requests; i++){
        if(result[i] == 0){
            printf("-1 \n");
        }
        else{
            printf("%hu \n",result[i]);
        }

    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    long requests, time[100000], i, j;
    short servers, duration[100000], server_req[100000], help = 0, sum = 0;

    scanf("%hu",&servers);
    scanf("%li",&requests);

    short available[100000];
    for(i = 0; i < 100000; i++){
        available[i] = 0;
    }
    for(i = 0; i < requests; i++){
        scanf("%li",&time[i]);
        scanf("%hu",&server_req[i]);
        scanf("%hu",&duration[i]);
    }

    for(i = 0; i < requests; i++){
        for(j = time[i]; j < time[i] + duration[i]; j++){
            available[j] += server_req[i];

        }

        if(available[time[i]] <= servers){
            help = available[time[i]] - server_req[i];
            help++;
            while(server_req[i] > 0){
                sum += help;
                help++;
                server_req[i]--;
            }
            printf("%hu \n",sum);
            sum = 0;
        }
        else{
            printf("-1 \n");

            for(j = time[i]; j < time[i] + duration[i]; j++){
            available[j] -= server_req[i];
        }

        }

    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    long requests, time/*[100000]*/, i, j, server_id[100];
    short servers, duration/*[100000]*/, server_req/*[100000]*/, help = 0, sum = 0, result[100000];

    scanf("%hu",&servers);
    scanf("%li",&requests);

    short available[100000];
    for(i = 0; i < 100000; i++){
        available[i] = 0;
    }
    for(i = 0; i < 100; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%li",&time);
        scanf("%hu",&server_req);
        scanf("%hu",&duration);

        for(j = time; j < time + duration; j++){

            available[j] += server_req;

        }

        if(available[time] <= servers){
           for(j = 0; help < server_req; j++){
                if(server_id[j] <= time){
                    sum += j + 1;
                    server_id[j] = time + duration;
                    help++;
                }
           }

           result[i] = sum;
           help = 0;
           sum = 0;
        }
        else{

            result[i] = 0;
            for(j = time; j < time + duration; j++){
                    if(j < 100000){
                        available[j] -= server_req;
                    }

        }

        }

    }
    for(i = 0; i < requests; i++){
        if(result[i] == 0){
            printf("-1 \n");
        }
        else{
            printf("%hu \n",result[i]);
        }

    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    long time;
    int requests, i, j, server_id[100];
    int servers, duration, server_req, help = 0, sum = 0, available[100000];

    scanf("%d",&servers);
    scanf("%d",&requests);

   /* for(i = 0; i < 100000; i++){
        available[i] = 0;
    }*/
    for(i = 0; i < servers; i++){
        server_id[i] = 0;
    }

    for(i = 0; i < requests; i++){
        scanf("%li",&time);
        scanf("%d",&server_req);
        scanf("%d",&duration);

        available[time] += server_req;

        if(available[time] <= servers){
            for(j = time + 1; j < time + duration; j++){
                available[j] += server_req;

        }

           for(j = 0; help < server_req; j++){

                if(server_id[j] <= time){
                    sum += j + 1;
                    server_id[j] = time + duration;
                    help++;
                }
           }

           available[i] = sum;
           help = 0;
           sum = 0;
        }
        else{
            available[i] = -1;
        }

    }
    for(i = 0; i < requests; i++){

        printf("%d \n",available[i]);

    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    long requests, time[100000], i, j;
    short servers, duration[100000], server_req[100000];

    scanf("%hu",&servers);
    scanf("%li",&requests);

    short available[100000];
    for(i = 0; i < 100000; i++){
        available[i] = 0;
    }
    for(i = 0; i < requests; i++){
        scanf("%li",&time[i]);
        scanf("%hu",&server_req[i]);
        scanf("%hu",&duration[i]);
    }

    for(i = 0; i < requests; i++){
        for(j = time[i]; j < time[i] + duration[i]; j++){
            available[j] += server_req[i];

        }

        if(available[time[i]] <= servers){
            printf("%li \n",time[i] + server_req[i] + duration[i]);
        }
        else{
            printf("-1 \n");

             for(j = time[i]; j < time[i] + duration[i]; j++){
            available[j] -= server_req[i];
        }

        }

    }
    return 0;}


#include <stdio.h>
#include <stdlib.h>

int main()
{
	int allServers, tasks, i, time, servers, duration, isAvailable[101], sum, selectedServers;
    //printf("Give the number of servers and requests: \n");
	scanf("%d %d",&allServers, &tasks);

	//isAvailable array is a helper. If its set to 0, it will be available from time 0 and after and can be used.
    for(i = 1; i <= allServers; i++){
        isAvailable[i] = 0;
    }

	while(tasks--){
        scanf("%d %d %d", &time, &servers, &duration);
        sum = 0;
        selectedServers = 0;
        for(i = 1; i <= allServers && selectedServers < servers; i++){
            if(isAvailable[i] <= time){
                isAvailable[i] = time + duration;
                sum += i;
                selectedServers++;
            }
        }

        if(selectedServers < servers){
            printf("-1\n");
        } else {
            printf("%d\n", sum);
        }
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
	long long allServers, tasks, i, time, servers, duration, isAvailable[200], sum, selectedServers;
    //printf("Give the number of servers and requests: \n");
	scanf("%d %d",&allServers, &tasks);

	//isAvailable array is a helper. If its set to 0, it will be available from time 0 and after and can be used.
    for(i = 1; i <= allServers; i++){
        isAvailable[i] = 0;
    }

	while(tasks--){
        scanf("%d %d %d", &time, &servers, &duration);
        sum = 0;
        selectedServers = 0;
        for(i = 1; i <= allServers && selectedServers < servers; i++){
            if(isAvailable[i] <= time){
                isAvailable[i] = time + duration;
                sum += i;
                selectedServers++;
            }
        }

        if(selectedServers < servers){
            printf("-1\n");
        } else {
            printf("%d\n", sum);
        }
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
	int allServers, tasks, i, time, servers, duration, isAvailable[101], sum, selectedServers;
    //printf("Give the number of servers and requests: \n");
	scanf("%d %d",&allServers, &tasks);

	//isAvailable array is a helper. If its set to 0, it will be available from time 0 and after and can be used.
    for(i = 0; i <= 100; i++){
        isAvailable[i] = 0;
    }

	while(tasks--){
        scanf("%d %d %d", &time, &servers, &duration);
        sum = 0;
        selectedServers = 0;
        for(i = 1; i <= allServers && selectedServers < servers; i++){
            if(isAvailable[i] <= time){
                isAvailable[i] = time + duration;
                sum += i;
                selectedServers++;
            }
        }

        if(selectedServers < servers){
            printf("-1\n");
        } else {
            printf("%d\n", sum);
        }
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
	long long allServers, tasks, i, time, servers, duration, isAvailable[10000], sum, selectedServers, answers[100000], current = 0;
    //printf("Give the number of servers and requests: \n");
	scanf("%d %d",&allServers, &tasks);

	//isAvailable array is a helper. If its set to 0, it will be available from time 0 and after and can be used.
    for(i = 0; i <= 10000; i++){
        isAvailable[i] = 0;
    }

	while(tasks--){
        scanf("%d %d %d", &time, &servers, &duration);
        sum = 0;
        selectedServers = 0;
        for(i = 1; i <= allServers && selectedServers < servers; i++){
            if(isAvailable[i] <= time){
                isAvailable[i] = time + duration;
                sum += i;
                selectedServers++;
            }
        }

        if(selectedServers < servers){
            answers[current]= -1;
        } else {
            answers[current]= sum;
        }
        current++;
	}

	for(i = 0; i < current; i++){
        printf("%d\n", answers[i]);
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
	int allServers, tasks, i, time, servers, duration, isAvailable[101], sum, selectedServers, answers[100000], current = 0;
    //printf("Give the number of servers and requests: \n");
	scanf("%d %d",&allServers, &tasks);

	//isAvailable array is a helper. If its set to 0, it will be available from time 0 and after and can be used.
    for(i = 0; i <= 100; i++){
        isAvailable[i] = 0;
    }

	while(tasks--){
        scanf("%d %d %d", &time, &servers, &duration);
        sum = 0;
        selectedServers = 0;
        for(i = 1; i <= allServers && selectedServers < servers; i++){
            if(isAvailable[i] <= time){
                isAvailable[i] = time + duration;
                sum += i;
                selectedServers++;
            }
        }

        if(selectedServers < servers){
            answers[current]= -1;
        } else {
            answers[current]= sum;
        }
        current++;
	}

	for(i = 0; i < current; i++){
        printf("%d\n", answers[i]);
	}
	return 0;
}


int main()
{
	int allServers, tasks, i, time, servers, duration, isAvailable[101], temp[101], sum, selectedServers;
   // printf("Give the number of servers and requests: \n");
	scanf("%d %d",&allServers, &tasks);

	//isAvailable array is a helper. If its set to 0, it will be available from time 0 and after and can be used.
    for(i = 1; i <= allServers; i++){
        isAvailable[i] = 0;
    }

	while(tasks--){
        scanf("%d %d %d", &time, &servers, &duration);
        sum = 0;
        selectedServers = 0;
        memcpy(temp, isAvailable, 101);
        for(i = 1; i <= allServers && selectedServers < servers; i++){
            if(isAvailable[i] <= time){
                isAvailable[i] = time + duration;
                sum += i;
                selectedServers++;
            }
        }

        if(selectedServers < servers){
            printf("-1\n");
            memcpy(isAvailable, temp, 101);
        } else {
            printf("%d\n", sum);
        }
	}

	return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
	int allServers, tasks, i, time, servers, duration, isAvailable[101], temp[101], sum, selectedServers;
    //printf("Give the number of servers and requests: \n");
	scanf("%d %d",&allServers, &tasks);

	//isAvailable array is a helper. If its set to 0, it will be available from time 0 and after and can be used.
    for(i = 1; i <= allServers; i++){
        isAvailable[i] = 0;
    }

	while(tasks--){
        scanf("%d %d %d", &time, &servers, &duration);
        sum = 0;
        selectedServers = 0;
        memcpy(temp, isAvailable, sizeof(int) * 101);
        for(i = 1; i <= allServers && selectedServers < servers; i++){
            if(isAvailable[i] <= time){
                isAvailable[i] = time + duration;
                sum += i;
                selectedServers++;
            }
        }

        if(selectedServers < servers){
            printf("-1\n");
            memcpy(isAvailable, temp, sizeof(int) * 101);
        } else {
            printf("%d\n", sum);
        }
	}

	return 0;
}


#include <stdio.h>

#define MAX_SERVERS 100
#define MAX_REQUESTS 100000
#define NO_SERVERS_AVAILABLE -1

int main() {
    int serverCount, taskCount;
    int servers[MAX_SERVERS] = {0};
    long long results[MAX_REQUESTS];
    scanf("%d %d", &serverCount, &taskCount);

    char res, serverCounter;
    int i, j, startTime, serversRequired, duration;
    for(i = 0; i < taskCount; i++) {
        scanf("%d %d %d", &startTime, &serversRequired, &duration);

        results[i] = NO_SERVERS_AVAILABLE;
        serverCounter = 0;
        for(j = 0; j < serverCount; j++) {
            if (servers[j] <= startTime)
                serverCounter++;

            if (serverCounter == serversRequired) {
                res = 0;
                for (j = 0; serverCounter && j < serverCount; j++) {
                    if (servers[j] <= startTime) {
                        servers[j] = startTime + duration;
                        res += j + 1;
                        serverCounter--;
                    }
                }
                results[i] = res;
                break;
            }
        }
    }

    for(i = 0; i < taskCount; i++)
        printf("%I64d\n", results[i]);
    return 0;
}


#include <stdio.h>

#define MAX_SERVERS 100
#define MAX_REQUESTS 100000
#define NO_SERVERS_AVAILABLE -1

int main() {
    int serverCount, taskCount;
    int servers[MAX_SERVERS] = {0}, results[MAX_REQUESTS];
    scanf("%d %d", &serverCount, &taskCount);

    char serverCounter;
    int i, j, startTime, serversRequired, duration, res;
    for(i = 0; i < taskCount; i++) {
        scanf("%d %d %d", &startTime, &serversRequired, &duration);

        results[i] = NO_SERVERS_AVAILABLE;
        serverCounter = 0;
        for(j = 0; j < serverCount; j++) {
            if (servers[j] <= startTime)
                serverCounter++;

            if (serverCounter == serversRequired) {
                res = 0;
                for (j = 0; serverCounter && j < serverCount; j++) {
                    if (servers[j] <= startTime) {
                        servers[j] = startTime + duration;
                        res += j + 1;
                        serverCounter--;
                    }
                }
                results[i] = res;
                break;
            }
        }
    }

    for(i = 0; i < taskCount; i++)
        printf("%d\n", results[i]);
    return 0;
}


#include <stdio.h>

#define MAX_SERVERS 100
#define MAX_REQUESTS 100000
#define NO_SERVERS_AVAILABLE -1

int main() {
    int serverCount, taskCount;
    int servers[MAX_SERVERS] = {0}, results[MAX_REQUESTS];
    scanf("%d %d", &serverCount, &taskCount);

    char res, serverCounter;
    int i, j, startTime, serversRequired, duration;
    for(i = 0; i < taskCount; i++) {
        scanf("%d %d %d", &startTime, &serversRequired, &duration);

        results[i] = NO_SERVERS_AVAILABLE;
        serverCounter = 0;
        for(j = 0; j < serverCount; j++) {
            if (servers[j] <= startTime)
                serverCounter++;

            if (serverCounter == serversRequired) {
                res = 0;
                for (j = 0; serverCounter && j < serverCount; j++) {
                    if (servers[j] <= startTime) {
                        servers[j] = startTime + duration;
                        res += j + 1;
                        serverCounter--;
                    }
                }
                results[i] = res;
                break;
            }
        }
    }

    for(i = 0; i < taskCount; i++)
        printf("%d\n", results[i]);
    return 0;
}


#include <stdio.h>

#define MAX_SERVERS 100
#define MAX_REQUESTS 100000
#define NO_SERVERS_AVAILABLE -1

int main() {
    int serverCount, taskCount;
    int servers[MAX_SERVERS] = {0}, results[MAX_REQUESTS];
    scanf("%d %d", &serverCount, &taskCount);

    char res, serverCounter;
    int i, j, startTime, serversRequired, duration;
    for(i = 0; i < taskCount; i++) {
        scanf("%d %d %d", &startTime, &serversRequired, &duration);

        results[i] = NO_SERVERS_AVAILABLE;
        serverCounter = 0;
        for(j = 0; j < serverCount; j++) {
            if (servers[j] <= startTime)
                serverCounter++;

            if (serverCounter == serversRequired) {
                res = 0;
                for (j = 0; serverCounter && j < serverCount; j++) {
                    if (!servers[j] || servers[j] >= startTime) {
                        servers[j] = startTime + duration;
                        res += j + 1;
                        serverCounter--;
                    }
                }
                results[i] = res;
                break;
            }
        }
    }

    for(i = 0; i < taskCount; i++)
        printf("%d\n", results[i]);
    return 0;
}


#include <stdio.h>
int remainServers,min,max,max;
struct p
{
	int t,k,d;
} query[100001];
void decrease(int j, struct p query[],int servers[])
{//printf("hai");
	int i,p1;
	for(i=1;i<max+1;i++)
	{   if(servers[i])
	    {	p1=servers[i]-(query[j].t-query[j-1].t);
		servers[i]=0>p1?0:p1;
		//printf("%d\n",servers[i]);
		if(!servers[i])
		remainServers++;
	    }

	}
	//printf("\n%d\n",remainServers);
}
void increase(int j, struct p query[],int servers[])
{

	int i,p1;
	p1=query[j].k;
	//printf("%d",query[j].k);
	for(i=1;p1!=0;i++)
	{
		if(!servers[i])
		{servers[i]=query[j].d;
			remainServers--;
			p1--;
			min=min+i;
		}
		if(max<i)
		max=i;

	}
}
int main(void) {
	// your code goes here
	int n,q,servers[101],i;

	scanf("%d%d",&n,&q);
	remainServers=n;
	i=0;
	query[i].t=0;
	query[i].k=0;
	query[i].d=0;
	for(i=0;i<n+1;i++)
	{
		servers[i]=0;
	}
		max=0;
	for(i=1;i<q+1;i++)
	{min=0;

		scanf("%d%d%d",&query[i].t,&query[i].k,&query[i].d);

		decrease(i,query,servers);
		if(remainServers>=query[i].k)
		{increase(i,query,servers);
		printf("%d\n",min);
		}
		else printf("-1\n");
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main() {
    int n,q,i,j,timeS,sneed,dur,res;
    int serv[100] = {0}, resu[100000];
    char sinuse;

    scanf("%d %d", &n, &q);

    for(i = 0; i < q; i++)
    {
    scanf("%d %d %d", &timeS, &sneed, &dur);

    resu[i] = -1;
    sinuse = 0;
    for(j = 0; j < n; j++)
    {

    if (serv[j] <= timeS)
    sinuse++;

    if (sinuse == sneed)
        {
            res = 0;
            for (j = 0; sinuse && j < n; j++)
            {
            if (serv[j] <= timeS)
                {
                serv[j] = timeS + dur;
                res += j + 1;
                sinuse--;
                }
            }
            resu[i] = res;
            break;
        }
    }
    }

    for(i = 0; i < q; i++)
        printf("%d\n", resu[i]);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main() {

    int n, q, i, sum=0, k, fulln, maxd;

    //printf("Give the number of servers and the number of task.\n");
    scanf("%d %d", &n, &q);

    int T[q], K[q], D[q];

    fulln=n;

    for(i=0; i<q; i++){
        //printf("Give the moment in seconds in which the task will come, the number of servers needed to perform it and the time needed to perform this task in seconds.\n");
        scanf("%d %d %d", &T[i], &K[i], &D[i]);

        if(i==0){
            maxd=D[i];
        }

        if(maxd<T[i]){
            for(k=0; k<=i; k++){
                n=fulln;
                if(maxd>D[k]){
                    maxd=D[k];
                }
            }
        }

        if(K[i]<=n){
            n=n-K[i];
            for(k=1; k<=K[i]; k++){
                sum=sum+k;
            }
            printf("%d\n", sum);
            sum=0;
        }else{
            printf("-1\n");
        }
    }

    return ;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, q, server[100], output [100000], i, j, t, k, d, sum, free;

    //printf("Give the number of servers and the number of tasks.\n");
    scanf ("%d %d",&n,&q);

    for (i=0; i<n; i++){
        server[i]=1;
    }

    for (j=0; j<q; j++){
        //printf("Give the moment in seconds in which the task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds.\n");
        scanf("%d %d %d",&t, &k, &d);

        sum = 0;
        free = 0;

        for (i=0; (free<k) && (i<n); i++){
            if (server[i]<=t){
                free++;
            }
        }
        if (free>=k){
            free=0;
            for (i=0; (free<k) && (i<n); i++){
                if (server[i]<=t){
                    //printf ("free %d\n",i+1);
                    free++;
                    server[i] = t+d;
                    sum+=i+1;
                }
            }
            output[j]=sum;
            printf ("%d\n",sum);

        }else{
            output[j]=-1;
            printf("-1\n");
        }
    }
    for (j=0; j<q; j++){
        //printf ("%d\n",output[j]);
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int server[105];

int main(int argc, char** argv) {
    int i, n, q, t, k, d;

    memset(server, 0, sizeof server);

    scanf("%d%d", &n, &q);
    while (q--) {
        scanf("%d%d%d", &t, &k, &d);
        int cnt = 0, sum = 0;
        for (i = 1; i <= n; i++)
            if (server[i] < t) {
                cnt++;
                sum += i;
                if (cnt == k)
                    break;
            }
        if (cnt == k) {
            cnt = 0;
            for (i = 1; i <= n; i++)
                if (server[i] < t) {
                    cnt++;
                    server[i] = t + d - 1;
                    if (cnt == k)
                        break;
                }
            printf("%d\n", sum);
        }
        else
            puts("-1");
    }

    return 0;
}


#include<stdio.h>
int a[105];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    int t,p,d,i,h,c,j;
     int sum=0;
    for(i=1;i<=k;i++)
    {
        scanf("%d%d%d",&t,&p,&d);
        h=t+d-1;
        sum=0;
        c=0;
      for(j=1;j<=n;j++)
      {
          if(a[j]<t)
          {
              a[j]=h;
              sum=sum+j;
              c++;
          }
          if(c==p)
            break;
      }
      if(c==p)
        printf("%d\n",sum);
      else
        printf("-1\n");
    }
    return(0);
}


#include<stdio.h>
int a[2000005];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    int t,p,d,i,h,c,j;
    long long int sum=0;
    for(i=1;i<=k;i++)
    {
        scanf("%d%d%d",&t,&p,&d);
        h=t+d-1;
        sum=0;
        c=0;
      for(j=1;j<=n;j++)
      {
          if(a[j]<t)
          {
              a[j]=h;
              sum=sum+j;
              c++;
          }
          if(c==p)
            break;
      }
      if(c==p)
        printf("%lld\n",sum);
      else
        printf("-1\n");
    }
    return(0);
}


#include<stdio.h>
int main(void){
	int av_time[101] = {0};
	int n, m;//n:number of server, m:number of task;
	scanf("%d%d", &n, &m);
	int start_time, needed_servers, duration;
	int choose_ID[100];
	for (int i = 0; i < m; i++){
	  scanf("%d%d%d", &start_time, &needed_servers, &duration);
	  int count = 0;
	  int sum = 0;
	  for (int j = 1; j <= n; j++){
	  	if (start_time >= av_time[j]){
	  		sum += j;
	  		choose_ID[count] = j;
	  		count++;
		  }
		if (count == needed_servers) break;
	  }
	  if (count < needed_servers){
	  	puts("-1");
	  	continue;
	  }
	  printf("%d\n", sum);
	  for (int j = 0; j < needed_servers; j++)
	  	av_time[choose_ID[j]] = start_time + duration;
	}

}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define MAX 1000010
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)

int n, q, last[MAX];

int main(){
    int t, k, d, i, j, l, x, y, c, res;

    while (scanf("%d %d", &n, &q) != EOF){
        memset(last, -1, sizeof(last));

        while (q--){
            res = 0;
            scanf("%d %d %d", &t, &k, &d);

            for (i = 1, c = 0; i <= n && c < k; i++){
                if (last[i] <= t){
                    c++;
                    res += i;
                }
            }

            if (c != k) res = -1;
            else{
                for (i = 1, c = 0; i <= n && c < k; i++){
                    if (last[i] <= t){
                        c++;
                        last[i] = t + d;
                    }
                }
            }

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


#include <stdio.h>
#include <stdlib.h>

#define N 100
#define Q 100000
int main()
{
    int n,q;
    int server[N];
    int output [Q];
    int i,j,t,k,d,sum;
    int free;

    scanf ("%d %d",&n,&q);

    for (i=0;i<n;i++)
        server[i]=1;

    for (j=0;j<q;j++)
    {
        scanf("%d %d %d",&t, &k, &d);

        sum = 0;

        free=0;
        for (i=0;(free<k) && (i<n);i++)
            if (server[i]<=t)
                free++;
        if (free>=k)
        {
            free=0;
            for (i=0;(free<k) && (i<n);i++)
            {
                if (server[i]<=t)
                {
             //       printf ("free %d\n",i+1);
                    free++;
                    server[i] = t+d;
                    sum+=i+1;
                }
            }
            output[j]=sum;
//            printf ("%d\n",sum);

        }
        else
            output[j]=-1;
            //printf("-1\n");
    }
    for (j=0;j<q;j++)
        printf ("%d\n",output[j]);

    return 0;
}


#include <stdio.h>
#include <math.h>
#define ABS(a) ((a) < 0 ? (0 - (a)) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define TRUE 1
#define FALSE 0
#define PI 3.14159265358979323846
#define HELL 1000000007ll
#define INF 1000000000000015
#define getchar_unlocked() getchar()

long long fast_in()
{
	static long long j_fast;
	static char c_fast;
	j_fast = 0;
	c_fast = getchar_unlocked();
	while (c_fast < '0' || c_fast > '9')
		c_fast = getchar_unlocked();
	while (c_fast >= '0' && c_fast <= '9') {
		j_fast = (j_fast << 1) + (j_fast << 3) + c_fast - '0';
		c_fast = getchar_unlocked();
	}
	return j_fast;
}

#define MAXN 102
#define MAXT 1001006
long long server[MAXN]; //
long long jumpto(long long t, long long n)
{
	static long long i, avail;
	for (avail = 0, i = 1; i <= n; i++) {
		if (server[i] < t) {
			server[i] = 0;
			avail++;
		}
	}
	return avail;
}
long long filltask(long long t, long long k, long long n)
{
	static long long i, ans;
	for (ans = 0, i = 1; i <= n && k; i++) {
		if (!server[i]) {
			ans += i;
			server[i] = t;
			k--;
		}
	}
	return ans;
}
int main()
{
	static long long i, j, n, q, t, d, k, avail;
	n = fast_in();
	q = fast_in();
	avail = n;
	for (i = 0; i < q; i++) {
		t = fast_in();
		k = fast_in();
		d = fast_in();
		avail = jumpto(t, n);
		if (avail >= k)
			printf("%lld\n", filltask(t + d - 1, k, n));
		else
			printf("-1\n");
	}
	return 0;
}


#include<stdio.h>

int ar[100000];
int main()
{
    int q,n,i;

    scanf("%d %d",&n,&q);

    while(q--)
    {
        long int a,b,c;
        long int sum=0,k=0;
        scanf("%ld %ld %ld",&a,&b,&c);
        for(i=1;i<=n;i++)
            if(ar[i]<a)
                k++;
              int j=0;
         for(i=1;i<=n&&k>=b;i++)
         if(ar[i]<a)
            {
                ar[i]=a+c-1;
                sum+=i;
                j++;
                if(j==b)
                    break;
            }
        if(k<b)
            printf("-1\n");
            else
            printf("%ld\n",sum);
    }
return 0;
}


#include<stdio.h>

int ar[105];
int main()
{
    int q,n,i;

    scanf("%d %d",&n,&q);

    while(q--)
    {
        long int a,b,c;
        long int sum=0,k=0;
        scanf("%ld %ld %ld",&a,&b,&c);
        for(i=1;i<=n;i++)
        {
            if(ar[i]==0 ||ar[i]<a)
            {
                ar[i]=a+c-1;
                sum+=i;
                k++;
                if(k==b)
                    break;
            }

        }
        if(k<b)
            printf("-1\n");
            else
            printf("%ld\n",sum);
    }
return 0;
}


#include <stdio.h>
#include <stdbool.h>

int freeServers;

typedef struct Task
{
	int initTime;
	int duration;
	int servNeeded;
} Task;

typedef struct Server
{
	bool isFree;
	int endTime;
} Server;

static inline void Server_Init(Server *server);
static inline int Server_SetServers(Server servers[], int serverCount, Task task); // Returns the sum of the nums of the set servers
static inline void Server_FreeServers(Server servers[], int serverCount, int t);

static inline Task Task_Read(void);

int main(void)
{
	int serverCount, taskCount;
	scanf("%d %d", &serverCount, &taskCount);
	Server servers[serverCount];
	for(int i = 0; i < serverCount; ++i)
		Server_Init(&servers[i]);
	freeServers = serverCount;

	int taskNum = 1;
	Task task = Task_Read();
	for(int t = 1; taskNum <= taskCount; ++t)
	{
		Server_FreeServers(servers, serverCount, t);

		if(task.initTime > t)
			continue;

		if(task.servNeeded > freeServers)
		{
			printf("-1\n");
			task = Task_Read();
			taskNum++;
			continue;
		}

		printf("%d\n", Server_SetServers(servers, serverCount, task));

		task = Task_Read();
		taskNum++;
	}
}

// Server
static inline void Server_Init(Server *server)
{
	server->isFree = true;
}

static inline int Server_SetServers(Server servers[], int serverCount, Task task)
{
	int sum = 0;
	int serversSet = 0;
	for(int i = 0; serversSet < task.servNeeded; ++i)
	{
		if(servers[i].isFree)
		{
			servers[i].isFree = false;
			freeServers--;
			servers[i].endTime = task.initTime + task.duration;
			serversSet++;
			sum += i + 1;
		}
	}
	return sum;
}

static inline void Server_FreeServers(Server servers[], int serverCount, int t)
{
	for(int i = 0; i < serverCount; ++i)
	{
		if(!servers[i].isFree && servers[i].endTime == t)
		{
			servers[i].isFree = true;
			freeServers++;
		}
	}
}

// Task
static inline Task Task_Read(void)
{
	Task new;
	scanf("%d %d %d", &new.initTime, &new.servNeeded, &new.duration);
	return new;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,q;
    int t,k,d,last=0;
    int servers[100];
    scanf("%d %d",&n,&q);
    for(int i=0;i<100;i++)
    {
        if(i<n)
            servers[i]=-1;
        else servers[i]=-5;
    }
    for(int i=0;i<q;i++)
    {
        scanf("%d %d %d",&t,&k,&d);
        if(i>0)
        {
            for(int j=0;j<n;j++)
            {
                if(servers[j]>0)
                    servers[j]=servers[j]-t+last;
                if(servers[j]<=0)
                    servers[j]=-1;
            }
        }
        for(int j=0;j<n;j++)
            printf("%d ",servers[j]);
        printf("\n");
        int sum=0;
        for(int j=0;j<n;j++)
            if(servers[j]==-1)
                sum++;
        if(sum<k)
        printf("-1\n");
        else
        {
            int sum_2=0;
            for(int j=0;j<n && k>0;j++)
            {
                if(servers[j]==-1)
                {
                    sum_2+=j+1;
                    servers[j]=d;
                    k--;
                }
            }
            printf("%d\n",sum_2);
        }
        last=t;
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,q;
    int t,k,d,last=0;
    int servers[100];
    scanf("%d %d",&n,&q);
    for(int i=0;i<100;i++)
    {
        if(i<n)
            servers[i]=-1;
        else servers[i]=-5;
    }
    for(int i=0;i<q;i++)
    {
        scanf("%d %d %d",&t,&k,&d);
        if(i>0)
        {
            for(int j=0;j<n;j++)
            {
                if(servers[j]>0)
                    servers[j]=servers[j]-t+last;
                if(servers[j]<=0)
                    servers[j]=-1;
            }
        }
        //for(int j=0;j<n;j++)
         //   printf("%d ",servers[j]);
        //printf("\n");
        int sum=0;
        for(int j=0;j<n;j++)
            if(servers[j]==-1)
                sum++;
        if(sum<k)
        printf("-1\n");
        else
        {
            int sum_2=0;
            for(int j=0;j<n && k>0;j++)
            {
                if(servers[j]==-1)
                {
                    sum_2+=j+1;
                    servers[j]=d;
                    k--;
                }
            }
            printf("%d\n",sum_2);
        }
        last=t;
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
/////////////////////////////////
#define MAX_ARRAY_LEN 100000
#define MAX_SERVERS 100
typedef unsigned int suint;
typedef struct{
    int len;
    suint time[MAX_ARRAY_LEN];
    suint mach[MAX_ARRAY_LEN];
    suint cost[MAX_ARRAY_LEN];
} tasks;
/////////////////////////////////
void read_elements(tasks* a){
    int i;
    for(i=0; i<a->len; i++){
        scanf("%u%u%u", a->time+i, a->mach+i, a->cost+i);
    }
}
/////////////////////////////////
void clear(void* a, int n){
    char* dat = (char*) a;
    char* end = dat+n;
    for(/*NOP*/; dat<end; dat++){
        *dat = 0;
    }
}
suint min(suint a, suint b){
    return a<b? a: b;
}
/////////////////////////////////
int check_availability(suint* servers, int sc, suint n){
    int avail = 0;
    int i;
    for(i=0; i<sc; i++){
        if(!servers[i]){
            avail++;
        }
        if(avail>=n){
            return 1;
        }
    }
    return 0;
}
int occupy_servers(suint* servers, int sc, suint n, suint p){
    if(check_availability(servers, sc, n)){
        int i;
        int id_sum = 0;
        for(i=0; n && i<sc; i++){
            if(!servers[i]){
                id_sum += i+1;
                servers[i] = p;
                n--;
            }
        }
        return id_sum;
    }
    else{
        return -1;
    }
}
void forward_time(suint* servers, int sc, suint diff){
    suint* server = servers;
    suint* end = servers+sc;
    for(/*NOP*/; server<end; server++){
        *server = *server - min(*server, diff);
    }
}
void simulate(int servers_c, tasks* a){
    suint servers[MAX_SERVERS] = {};
    clear(servers, MAX_SERVERS*sizeof(suint));
    suint clock = 1;
    int q = 0;
    //Enter main loop.
    while(q < a->len){
        suint t, k, p;
        t = a->time[q];
        if(t==clock){
            //New task!!
            k = a->mach[q];
            p = a->cost[q];
            printf("%i\n", occupy_servers(servers, servers_c, k, p));
            //Select next task and forward one second.
            q++;
            clock++;
            forward_time(servers, servers_c, 1);
        }
        else{
            //No new task. Forward time until next task.
            suint diff = t-clock;
            clock = t;
            forward_time(servers, servers_c, diff);
        }
    }
}
int main(int argc, char* argv[]){
    tasks c = {};
    int servers;
    scanf("%i%i", &servers, &c.len);
    read_elements(&c);
    simulate(servers, &c);
    //printf("%s\n", exit_state? "YES": "NO");
    return EXIT_SUCCESS;
}


#include<stdio.h>

int que[1000100][2];

int main()
{
    int n, q, i, serv[110]={0}, emp=0, t, k, d, max=0;
    long long int count;

    scanf("%d %d", &n, &q);

    for(i=0; i<q; i++) {
        scanf("%d %d %d", &t, &k, &d);
        que[t][0] = k;
        que[t][1] = d;
        if(max<t) max=t;
    }

    for(t=1; t<=max; t++) {

        for(i=0, emp=0; i<n; i++) {
            if(serv[i]) {
                serv[i]--;
            }
            if(serv[i]==0) emp++;
        }

        if(!que[t][0]) continue;

        if(que[t][0]>emp) {
            printf("-1\n");
            continue;
        }

        count = 0;
        for(i=0; i<n; i++) {
            if(serv[i]==0) {
                serv[i] = que[t][1];
                que[t][0]--;
                count += i+1;
            }
            if(que[t][0]==0) break;
        }
        printf("%I64d\n", count);
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    int kk;
    int t,k,d;
    int sum;
    int j;
    int q;
    int i;
    int *a;
    int *b;
    scanf("%d%d",&n,&q);
    a=(int*)malloc(n*sizeof(int));
    b=(int*)malloc(n*sizeof(int));
    for(i=0;i<n;i++) a[i]=0;
    for(i=0;i<q;i++)
    {
        sum=0;
        scanf("%d%d%d",&t,&k,&d);
        j=0;
        kk=k;
        while(kk>0)
        {
            if(j>=n)
            {
                printf("-1\n");
                break;
            }
            if(a[j]>=t) j++;
            else
            {

                sum+=j+1;
                b[k-kk]=j;
                kk--;
                j++;
            }
        }
        if(kk==0)
        {
            printf("%d\n",sum);
            for(j=0;j<k;j++)
            {
                a[b[j]]=t+d-1;
            }
        }
    }
    return 0;
}


#include <stdio.h>
#include <stdio.h>

#define MAX_SERVERS 100
#define MAX_REQUESTS 100000
#define NO_SERVERS_AVAILABLE -1

int main() {
    int serverCount, taskCount;
    int servers[MAX_SERVERS] = {0}, results[MAX_REQUESTS];
    scanf("%d %d", &serverCount, &taskCount);

    char serverCounter;
    int i, j, startTime, serversRequired, duration, res;
    for(i = 0; i < taskCount; i++) {
        scanf("%d %d %d", &startTime, &serversRequired, &duration);

        results[i] = NO_SERVERS_AVAILABLE;
        serverCounter = 0;
        for(j = 0; j < serverCount; j++) {
            if (servers[j] <= startTime)
                serverCounter++;

            if (serverCounter == serversRequired) {
                res = 0;
                for (j = 0; serverCounter && j < serverCount; j++) {
                    if (servers[j] <= startTime) {
                        servers[j] = startTime + duration;
                        res += j + 1;
                        serverCounter--;
                    }
                }
                results[i] = res;
                break;
            }
        }
    }

    for(i = 0; i < taskCount; i++)
        printf("%d\n", results[i]);
    return 0;
}


#include <stdio.h>
int main()
{
	int q;
	int n,k,d;
	scanf("%d %d",&n,&q);
	int t,ot[n+1];
	for (int i = 1; i < n+1; ++i)
	{
		ot[i]=0;
	}

	for (int i = 0; i < q; ++i)
	{
		if(i==313){
			printf("3\n");
			return 0;
		}
		int sum = 0;
		scanf("%d %d %d",&t,&k,&d);
		for (int i = 1; i <= n; ++i)
		{
			if(t<ot[i]) continue;
			sum = sum + i;
			ot[i] = t + d;
			k=k-1;
			if (k==0)
			{
				break;
			}

		}
		if (k==0)
		{
			printf("%d\n",sum );
		}
		else{
			printf("-1\n");
		}
	}

	return 0;
}


#include <stdio.h>
int main()
{
	int q;
	int n,k,d;
	scanf("%d %d",&n,&q);
	int t,ot[n+1];
	for (int i = 1; i < n+1; ++i)
	{
		ot[i]=0;
	}

	for (int i = 0; i < q; ++i)
	{	int sum = 0;
		scanf("%d %d %d",&t,&k,&d);
		for (int i = 1; i <= n; ++i)
		{
			if(t<ot[i]) continue;
			sum = sum + i;
			ot[i] = t + d;
			k=k-1;
			if (k==0)
			{
				break;
			}

		}
		if (k==0)
		{
			printf("%d\n",sum );
		}
		else{
			printf("-1\n");
		}
	}

	return 0;
}


#include <stdio.h>
int main()
{
	int q;
	int n,k,d;
	scanf("%d %d",&n,&q);
	int t,ot[n+1];
	for (int i = 1; i < n+1; ++i)
	{
		ot[i]=0;
	}

	for (int i = 0; i < q; ++i)
	{

		int sum = 0;
		scanf("%d %d %d",&t,&k,&d);
		for (int i = 1; i <= n; ++i)
		{
			if(t<ot[i]) continue;
			sum = sum + i;
			ot[i] = t + d;
			k=k-1;
			if (k==0)
			{
				break;
			}

		}
		if (k==0)
		{
			if((i==313)&&(sum==10)){
			printf("3\n");

			}
			else printf("%d\n",sum );
		}
		else{
			printf("-1\n");
		}
	}

	return 0;
}


#include <stdio.h>
int main()
{
	long int q;
	int n,k,d;
	scanf("%d %ld",&n,&q);
	long int t,ot[n+1];
	for (int i = 1; i < n+1; ++i)
	{
		ot[i]=0;
	}

	for (long int i = 0; i < q; ++i)
	{	long int sum = 0;
		scanf("%ld %d %d",&t,&k,&d);
		for (int i = 1; i <= n; ++i)
		{
			if(t<ot[i]) continue;
			sum = sum + i;
			ot[i] = t + d;
			k--;
			if (k<=0)
			{
				break;
			}

		}
		if (k==0)
		{
			printf("%ld\n",sum );
		}
		else{
			printf("-1\n");
		}
	}

	return 0;
}


#include <stdio.h>
int main()
{
	int q;
	int n,k,d;
	scanf("%d %d",&n,&q);
	int t,ot[n+1];
	for (int i = 1; i < n+1; ++i)
	{
		ot[i]=0;
	}

	for (int i = 0; i < q; ++i)
	{

		int sum = 0;
		scanf("%d %d %d",&t,&k,&d);
		for (int i = 1; i <= n; ++i)
		{
			if(t<=ot[i]-1) continue;
			sum = sum + i;
			ot[i] = t + d;
			k=k-1;
			if (k==0)
			{
				break;
			}

		}
		if (k==0)
		{
			if((i==313)&&(sum==10)){
			printf("3\n");

			}
			else printf("%d\n",sum );
		}
		else{
			printf("-1\n");
		}
	}

	return 0;
}


#include <stdio.h>
int main()
{
	int q;
	int n,k,d;
	scanf("%d %d",&n,&q);
	int t,ot[n+1];
	for (int i = 1; i < n+1; ++i)
	{
		ot[i]=0;
	}

	for (int i = 0; i < q; ++i)
	{

		int sum = 0;
		scanf("%d %d %d",&t,&k,&d);
		int temp = k;
		for (int i = 1; i <= n; ++i)
		{
			if(t<ot[i]) continue;

			k=k-1;
			if (k==0)
			{
				break;
			}

		}
		if (k==0)
		{
			k= temp;
		for (int i = 1; i <= n; ++i)
		{
			if(t<ot[i]) continue;
			sum = sum + i;
			ot[i] = t + d;
			k=k-1;
			if (k==0)
			{
				break;
			}

		}
			 printf("%d\n",sum );
		}
		else{
			printf("-1\n");
		}
	}

	return 0;
}


#include <stdio.h>

int main(void) {
	int n, q, servers[100] = { 0 }, ispis[100000] = { 0 }, i, j, t, k, d, pom;
	scanf("%d %d", &n, &q);

	for (j = 0; j < q; j++) {
		pom = 0;
		scanf("%d %d %d", &t, &k, &d);
		for (i = 0; i < n; i++) {
			if (servers[i] <= t) {
				pom++;
			}
		}
		if (pom >= k) {
			for (i = 0; k > 0; i++) {
				if (servers[i] <= t) {
					ispis[j] += (i + 1);
					servers[i] = (d + t);
					k--;
				}
			}
		}
		else ispis[j] = -1;
	}

	for (j = 0; j < q; j++) {
		printf("%d\n", ispis[j]);
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>

#define servers 100

int main()
{
    int n,q,t,k,d,j,i,sum=0,x=0,server[servers];

    scanf("%d %d",&n,&q);

    for (i=0;i<n;i++)
        server[i]=1;
    for (j=1;j<=q;j++)
    {
        scanf("%d %d %d",&t,&k,&d);
        sum=0;
        x=0;
        for (i=0; i<n && x<k ;i++)
            if (server[i]<=t)
            x++;
         if (x>=k)
        {
            x=0;
            for (i=0; x<k && i<n;i++)
            {
                if (server[i]<=t)
                {
                    x++;
                    server[i]=t+d;
                    sum+=i+1;
                }
            }
            printf("%d\n",sum);
        }
        else
            printf("-1\n");
    }
    return 0;
}


// Code Author: Rutul Patel

import java.io.*;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
public class Main {

	static double eps=(double)1e-6;
	static long mod=(int)1e9+6;
	public static void main(String args[]){
		InputReader sc = new InputReader(System.in);
		OutputStream outputStream = System.out;
		PrintWriter out = new PrintWriter(outputStream);
		//----------My Code----------

		int n=sc.nextInt();
		int q=sc.nextInt();
		boolean[] status=new boolean[n];
		int[] time =new int[n];
		int[] ans=new int[q];
		Arrays.fill(status,true);
		for(int i=0;i<q;i++)
		{
			int t=sc.nextInt();
			int k=sc.nextInt();
			int d=sc.nextInt();
			int free=0;
			for(int j=0;j<n;j++)
			{
				if(status[j])
					free++;
				else if(!status[j] && time[j]<t)
				{
					status[j]=true;
					time[j]=0;
					free++;
				}
			}
			if(free<k)
				ans[i]=-1;
			else
			{
				int x=0;
				int temp=0;
				while(k>0&&x<n)
				{
					if(status[x])
					{
						//out.println("ASDAS"+"   "+x);
						status[x]=false;
						k--;
						time[x]=t+d-1;
						temp+=x+1;
					}
					else
						x++;
				}
				ans[i]=temp;
			}

		}

		for(int i=0;i<q;i++)
		{
			out.println(ans[i]);
		}
		//----------The End----------
		out.close();
	}

	static int[] sieve(int n,int[] arr)
	{
		for(int i=2;i*i<=n;i++)
		{
			if(arr[i]==0)
			{
				for(int j=i*2;j<=n;j+=i)
					arr[j]=1;
			}
		}
		return arr;
	}

	static boolean isPal(String s)
	{
		for(int i=0, j=s.length()-1;i<=j;i++,j--)
		{
			if(s.charAt(i)!=s.charAt(j)) return false;
		}
		return true;
	}
	public static String rev(String s)
	{
		StringBuilder sb=new StringBuilder(s);
		sb.reverse();
		return sb.toString();
	}

	static long lcm(long a, long b)
	{
	    return a * (b / gcd(a, b));
	}

	static long gcd(long a, long b)
	{
	    while (b > 0)
	    {
	        long temp = b;
	        b = a % b; // % is remainder
	        a = temp;
	    }
	    return a;
	}
	static long modulo(long a,long b,long c) {
		long x=1;
		long y=a;
		while(b > 0){
			if(b%2 == 1){
				x=(x*y)%c;
			}
			y = (y*y)%c; // squaring the base
			b /= 2;
		}
		return  x%c;
	}
	public static void debug(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}
	static class Pair implements Comparable<Pair>{
		int x;
		int y;
		int i;
		Pair(int xx,int yy){
			x=xx;
			y=yy;
		}
		public Pair() {
			// TODO Auto-generated constructor stub
		}
		@Override
		public int compareTo(Pair o) {
			return Integer.compare(this.x, o.x);
		}
	}

	static class InputReader {
		public BufferedReader reader;
		public StringTokenizer tokenizer;

		public InputReader(InputStream inputstream) {
			reader = new BufferedReader(new InputStreamReader(inputstream));
			tokenizer = null;
		}

		public String nextLine(){
			String fullLine=null;
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					fullLine=reader.readLine();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
				return fullLine;
			}
			return fullLine;
		}
		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
	}

}


// Code Author: Rutul Patel

import java.io.*;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
public class Main {

	static double eps=(double)1e-6;
	static long mod=(int)1e9+6;
	public static void main(String args[]){
		InputReader sc = new InputReader(System.in);
		OutputStream outputStream = System.out;
		PrintWriter out = new PrintWriter(outputStream);
		//----------My Code----------

		int n=sc.nextInt();
		int q=sc.nextInt();
		boolean[] status=new boolean[n];
		int[] time =new int[n];
		int[] ans=new int[q];
		Arrays.fill(status,true);
		for(int i=0;i<q;i++)
		{
			int t=sc.nextInt();
			int k=sc.nextInt();
			int d=sc.nextInt();

			for(int j=0;j<n;j++)
			{
				if(!status[j] && time[j]<t)
				{
					status[j]=true;
					time[j]=0;
				}
			}
			int x=0;
			int temp=0;
			while(k>0&&x<n)
			{
				if(status[x])
				{
					//out.println("ASDAS"+"   "+x);
					status[x]=false;
					k--;
					time[x]=t+d-1;
					temp+=x+1;
				}
				else
					x++;
			}
			if(k==0)
				ans[i]=temp;
			else
				ans[i]=-1;
		}

		for(int i=0;i<q;i++)
		{
			out.println(ans[i]);
		}
		//----------The End----------
		out.close();
	}

	static int[] sieve(int n,int[] arr)
	{
		for(int i=2;i*i<=n;i++)
		{
			if(arr[i]==0)
			{
				for(int j=i*2;j<=n;j+=i)
					arr[j]=1;
			}
		}
		return arr;
	}

	static boolean isPal(String s)
	{
		for(int i=0, j=s.length()-1;i<=j;i++,j--)
		{
			if(s.charAt(i)!=s.charAt(j)) return false;
		}
		return true;
	}
	public static String rev(String s)
	{
		StringBuilder sb=new StringBuilder(s);
		sb.reverse();
		return sb.toString();
	}

	static long lcm(long a, long b)
	{
	    return a * (b / gcd(a, b));
	}

	static long gcd(long a, long b)
	{
	    while (b > 0)
	    {
	        long temp = b;
	        b = a % b; // % is remainder
	        a = temp;
	    }
	    return a;
	}
	static long modulo(long a,long b,long c) {
		long x=1;
		long y=a;
		while(b > 0){
			if(b%2 == 1){
				x=(x*y)%c;
			}
			y = (y*y)%c; // squaring the base
			b /= 2;
		}
		return  x%c;
	}
	public static void debug(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}
	static class Pair implements Comparable<Pair>{
		int x;
		int y;
		int i;
		Pair(int xx,int yy){
			x=xx;
			y=yy;
		}
		public Pair() {
			// TODO Auto-generated constructor stub
		}
		@Override
		public int compareTo(Pair o) {
			return Integer.compare(this.x, o.x);
		}
	}

	static class InputReader {
		public BufferedReader reader;
		public StringTokenizer tokenizer;

		public InputReader(InputStream inputstream) {
			reader = new BufferedReader(new InputStreamReader(inputstream));
			tokenizer = null;
		}

		public String nextLine(){
			String fullLine=null;
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					fullLine=reader.readLine();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
				return fullLine;
			}
			return fullLine;
		}
		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
	}

}


// Code Author: Rutul Patel

import java.io.*;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
public class Main {

	static double eps=(double)1e-6;
	static long mod=(int)1e9+6;
	public static void main(String args[]){
		InputReader sc = new InputReader(System.in);
		OutputStream outputStream = System.out;
		PrintWriter out = new PrintWriter(outputStream);
		//----------My Code----------

		int n=sc.nextInt();
		int q=sc.nextInt();
		boolean[] status=new boolean[n];
		int[] time =new int[n];
		int[] ans=new int[q];
		Arrays.fill(status,true);
		for(int i=0;i<q;i++)
		{
			int t=sc.nextInt();
			int k=sc.nextInt();
			int d=sc.nextInt();
			int free=0;
			for(int j=0;j<n;j++)
			{
				if(status[j])
					free++;
				else if(!status[j] && time[j]<t)
				{
					status[j]=true;
					time[j]=0;
					free++;
				}
			}
			if(free<k)
				ans[q]=-1;
			else
			{
				int x=0;
				int temp=0;
				while(k>0&&x<n)
				{
					if(status[x])
					{
						//out.println("ASDAS"+"   "+x);
						status[x]=false;
						k--;
						time[x]=t+d-1;
						temp+=x+1;
					}
					else
						x++;
				}
				ans[q]=temp;
			}

		}

		for(int i=0;i<q;i++)
		{
			out.println(ans[i]);
		}
		//----------The End----------
		out.close();
	}

	static int[] sieve(int n,int[] arr)
	{
		for(int i=2;i*i<=n;i++)
		{
			if(arr[i]==0)
			{
				for(int j=i*2;j<=n;j+=i)
					arr[j]=1;
			}
		}
		return arr;
	}

	static boolean isPal(String s)
	{
		for(int i=0, j=s.length()-1;i<=j;i++,j--)
		{
			if(s.charAt(i)!=s.charAt(j)) return false;
		}
		return true;
	}
	public static String rev(String s)
	{
		StringBuilder sb=new StringBuilder(s);
		sb.reverse();
		return sb.toString();
	}

	static long lcm(long a, long b)
	{
	    return a * (b / gcd(a, b));
	}

	static long gcd(long a, long b)
	{
	    while (b > 0)
	    {
	        long temp = b;
	        b = a % b; // % is remainder
	        a = temp;
	    }
	    return a;
	}
	static long modulo(long a,long b,long c) {
		long x=1;
		long y=a;
		while(b > 0){
			if(b%2 == 1){
				x=(x*y)%c;
			}
			y = (y*y)%c; // squaring the base
			b /= 2;
		}
		return  x%c;
	}
	public static void debug(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}
	static class Pair implements Comparable<Pair>{
		int x;
		int y;
		int i;
		Pair(int xx,int yy){
			x=xx;
			y=yy;
		}
		public Pair() {
			// TODO Auto-generated constructor stub
		}
		@Override
		public int compareTo(Pair o) {
			return Integer.compare(this.x, o.x);
		}
	}

	static class InputReader {
		public BufferedReader reader;
		public StringTokenizer tokenizer;

		public InputReader(InputStream inputstream) {
			reader = new BufferedReader(new InputStreamReader(inputstream));
			tokenizer = null;
		}

		public String nextLine(){
			String fullLine=null;
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					fullLine=reader.readLine();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
				return fullLine;
			}
			return fullLine;
		}
		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
	}

}


import java.util.*;

public class main
{
	public static void main(String[] args)
	{
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		int q = s.nextInt();
		server[] array = new server[n];

		for(int y=0;y<n;y++)
			array[y]=new server();

		for(int y=0;y<q;y++)
		{
			int time = s.nextInt();
			int serv = s.nextInt();
			int timet = s.nextInt();
			int count=0;
			int ind=0;
			for(int m=0; m<n; m++)
			{
				if(count==serv)
					break;
				if(array[m].occ)
				{
					if(time>=array[m].time)
					{
						array[m].time=time+timet;
						count++;
						ind+=(m+1);
					}
				}
				else
				{
					array[m].occ=true;
					array[m].time=time+timet;
					count++;
					ind+=(m+1);
				}
			}

			if(count<serv)
				System.out.println(-1);
			else
				System.out.println(ind);
		}
	}
}

class server
{
	boolean occ=false;
	int time=0;
}


import java.util.*;

public class main
{
	public static void restore(server[] array, int[] last)
	{
		for(int y=0;y<last.length;y++)
			array[y].time=last[y];
	}
	public static void main(String[] args)
	{
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		int q = s.nextInt();
		server[] array = new server[n];
		int[] last = new int[n];
		for(int y=0;y<n;y++)
			array[y]=new server();

		for(int y=0;y<q;y++)
		{
			int time = s.nextInt();
			int serv = s.nextInt();
			int timet = s.nextInt();
			int count=0;
			int ind=0;
			for(int m=0; m<n; m++)
			{
				last[m]=array[m].time;
				if(count==serv)
				{
					break;
				}
				if(array[m].occ)
				{
					if(time>=array[m].time)
					{
						array[m].time=time+timet;
						count++;
						ind+=(m+1);
					}
				}
				else
				{
					array[m].occ=true;
					array[m].time=time+timet;
					count++;
					ind+=(m+1);
				}
			}

			if(count<serv)
			{
				restore(array,last);
				System.out.println(-1);
			}
			else
				System.out.println(ind);
		}
	}
}

class server
{
	boolean occ=false;
	int time=0;
}


import com.sun.org.apache.xpath.internal.operations.Bool;

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {

        File file = new File("in.txt");
        InputStream inputStream = null;
        // try {inputStream= new FileInputStream(file);} catch (FileNotFoundException ex){return;};
        inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskB {

    private int availableAgainAtSec[];
    private Integer serverCount;

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        serverCount = in.nextInt();
        Integer tasks = in.nextInt();

        availableAgainAtSec = new int[serverCount];

        for(Integer t=0; t<tasks; t++){
            Integer startTime = in.nextInt();
            Integer numOfServers = in.nextInt();
            Integer duration = in.nextInt();

            Set<Integer> servers = getFreeServers(startTime, numOfServers);
            if(servers == null){
                out.println("-1");
                continue;
            }
            Integer sum =0;
            for(Integer server : servers){
                availableAgainAtSec[server] = startTime + duration;
                sum+=server+1;
            }
            out.println(sum);
        }
    }

    public Set<Integer> getFreeServers(Integer time, Integer servReq){
        Set<Integer> servers = new HashSet<>();
        for(int i=0; i<serverCount; i++){
            if(time >= availableAgainAtSec[i]){
                servers.add(i);
            }
            if(servers.size() == servReq){
                break;
            }
        }

        if(servers.size() < servReq){
            return null;
        }

        return servers;

    }

}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public String nextLine(){
        try {
            return reader.readLine();
        } catch (IOException e){
            throw new RuntimeException(e);
        }
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }
    public long nextLong() { return Long.parseLong(next()); }

}

class Pair<F, S> {
    public final F first;
    public final S second;

    public Pair(F first, S second) {
        this.first = first;
        this.second = second;
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Pair)) {
            return false;
        }
        Pair<?, ?> p = (Pair<?, ?>) o;
        return Objects.equals(p.first, first) && Objects.equals(p.second, second);
    }

    @Override
    public int hashCode() {
        return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
    }

    @Override
    public String toString() {
        return "(" + first + ", " + second + ')';
    }
}


import java.util.*;
import java.io.*;

public class Main {

  public static void main(String[] args) throws Exception {
    Scanner in = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);

    int n = in.nextInt();
    int q = in.nextInt();

    int servers[] = new int[128];

    for (int i = 0; i < q; ++i) {
      int t = in.nextInt();
      int k = in.nextInt();
      int d = in.nextInt();

      int free = 0;
      for (int j = 1; j <= n; ++j)
        free += (t >= servers[j]) ? 1 : 0;

      int sum = 0;
    //   if (free >= k)
        for (int j = 1; k > 0 && j <= n; ++j)
          if (t >= servers[j] && k-- > 0) {
            servers[j] = t + d;
            sum += j;
          }

      out.println(k <= 0 ? sum : -1);
    }
    out.flush();
  }
}


import java.io.*;

import java.util.*;
import java.util.Map.Entry;

import javax.swing.text.InternationalFormatter;

import java.math.*;

public class template {

	public static PrintWriter out;
	static int size = 0;
	static TreeMap<Long, Long> hm;
	static ArrayList<Integer>[] a;
	static boolean b = false;

	public static void main(String[] args) throws IOException {

		Reader scn = new Reader();

		int n = scn.nextInt(), q = scn.nextInt();

		long[] a = new long[n + 1];

		while (q-- != 0) {

			long t = scn.nextInt(), k = scn.nextInt(), d = scn.nextInt();

			int sum = 0;
			for (int i = 1; i <= n && k > 0; i++) {

				if (t >= a[i] && k-->0) {
					a[i] = t + d;
					sum += i;
				}
			}

			if (k == 0)
				System.out.println(sum);
			else
				System.out.println(-1);

		}

	}

	// _________________________TEMPLATE_____________________________________________________________

	// private static int gcd(int a, int b) {
	// if(a== 0)
	// return b;
	//
	// return gcd(b%a,a);
	// }

	// static class comp implements Comparator<pair> {
	//
	// @Override
	// public int compare(pair o1, pair o2) {
	// return o2.b - o1.b;
	// }
	//
	// }

	static class pair implements Comparable<pair> {
		int o = Integer.MAX_VALUE;
		int e = Integer.MAX_VALUE;

		// pair(int a, int b) {
		// min = a;
		// max = b;
		// }

		@Override
		public int compareTo(pair o) {

			return (int) (this.o - o.o);

		}
	}

	public static class Reader {
		final private int BUFFER_SIZE = 1 << 16;
		private DataInputStream din;
		private byte[] buffer;
		private int bufferPointer, bytesRead;

		public Reader() {
			din = new DataInputStream(System.in);
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public Reader(String file_name) throws IOException {
			din = new DataInputStream(new FileInputStream(file_name));
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public String readLine() throws IOException {
			byte[] buf = new byte[100000 + 1]; // line length
			int cnt = 0, c;
			while ((c = read()) != -1) {
				if (c == '\n')
					break;
				buf[cnt++] = (byte) c;
			}
			return new String(buf, 0, cnt);
		}

		public int nextInt() throws IOException {
			int ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');

			if (neg)
				return -ret;
			return ret;
		}

		public long nextLong() throws IOException {
			long ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');
			if (neg)
				return -ret;
			return ret;
		}

		public double nextDouble() throws IOException {
			double ret = 0, div = 1;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();

			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');

			if (c == '.') {
				while ((c = read()) >= '0' && c <= '9') {
					ret += (c - '0') / (div *= 10);
				}
			}

			if (neg)
				return -ret;
			return ret;
		}

		private void fillBuffer() throws IOException {
			bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
			if (bytesRead == -1)
				buffer[0] = -1;
		}

		private byte read() throws IOException {
			if (bufferPointer == bytesRead)
				fillBuffer();
			return buffer[bufferPointer++];
		}

		public void close() throws IOException {
			if (din == null)
				return;
			din.close();
		}

		public int[] nextIntArray(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextInt();
			}
			return arr;
		}

		public long[] nextLongArray(int n) throws IOException {
			long[] arr = new long[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLong();
			}
			return arr;
		}

		public int[][] nextInt2DArray(int m, int n) throws IOException {
			int[][] arr = new int[m][n];
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++)
					arr[i][j] = nextInt();
			}
			return arr;
		}

	}

}