#include <bits/stdc++.h>
using namespace std;
template <typename T>
T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9') break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9') break;
  }
  return (ng ? -n : n);
}
template <typename T>
inline T POW(T B, T P) {
  if (P == 0) return 1;
  if (P & 1)
    return B * POW(B, P - 1);
  else
    return (POW(B, P / 2) * POW(B, P / 2));
}
template <typename T>
inline T Gcd(T a, T b) {
  if (a < 0) return Gcd(-a, b);
  if (b < 0) return Gcd(a, -b);
  return (b == 0) ? a : Gcd(b, a % b);
}
template <typename T>
inline T Lcm(T a, T b) {
  if (a < 0) return Lcm(-a, b);
  if (b < 0) return Lcm(a, -b);
  return a * (b / Gcd(a, b));
}
long long Bigmod(long long base, long long power, long long MOD) {
  long long ret = 1;
  while (power) {
    if (power & 1) ret = (ret * base) % MOD;
    base = (base * base) % MOD;
    power >>= 1;
  }
  return ret;
}
bool isVowel(char ch) {
  ch = toupper(ch);
  if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
    return true;
  return false;
}
template <typename T>
long long int isLeft(T a, T b, T c) {
  return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
}
long long ModInverse(long long number, long long MOD) {
  return Bigmod(number, MOD - 2, MOD);
}
bool isConst(char ch) {
  if (isalpha(ch) && !isVowel(ch)) return true;
  return false;
}
int toInt(string s) {
  int sm;
  stringstream second(s);
  second >> sm;
  return sm;
}
struct z {
  long long int t;
  int s, e, id;
};
z a[200007];
bool cmp(z a, z b) { return (a.t < b.t); }
vector<int> wait[200007];
vector<int> lift[200007];
int Tree[200007], N;
void Update(int id, int v) {
  while (id <= N) {
    Tree[id] += v;
    id += id & -id;
  }
}
int Query(int id) {
  int ans = 0;
  while (id > 0) {
    ans += Tree[id];
    id -= id & -id;
  }
  return ans;
};
int QryL(int id) {
  int tot = Query(id);
  int lo = 1, hi = id;
  int ans = -1;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    int tp = Query(mid);
    if (tp == tot) {
      ans = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  return id - ans;
}
int QryR(int id) {
  int tot = Query(id);
  int lo = id, hi = N;
  int ans = -1;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    int tp = Query(mid);
    if (tp == tot) {
      ans = mid;
      lo = mid + 1;
    } else
      hi = mid - 1;
  }
  ans++;
  return ans - id;
}
long long int ans[200007];
int main() {
  int n, m;
  n = in<int>(), m = in<int>();
  N = 100005;
  for (int i = 0; i < n; i++) {
    a[i].t = in<long long int>();
    a[i].s = in<int>(), a[i].e = in<int>();
    a[i].id = i;
  }
  sort(a, a + n, cmp);
  long long int tme = 0;
  int pos = 1;
  int ln = 0;
  int tot = 0;
  a[n].t = 1e9 + 10;
  int top = 3000000;
  while (true) {
    while (ln < n && a[ln].t <= tme) {
      wait[a[ln].s].push_back(ln);
      Update(a[ln].s, 1);
      ln++;
    }
    for (int i = 0; i < lift[pos].size(); i++) {
      ans[lift[pos][i]] = tme;
      tot++;
      Update(pos, -1);
    }
    lift[pos].clear();
    for (int i = 0; i < wait[pos].size(); i++) {
      Update(pos, -1);
      Update(a[wait[pos][i]].e, 1);
      lift[a[wait[pos][i]].e].push_back(a[wait[pos][i]].id);
    }
    wait[pos].clear();
    int sm = Query(pos);
    int sm1 = Query(N) - sm;
    long long int mn = 1e9;
    if (ln < n) mn = min(mn, a[ln].t - tme);
    if ((sm + sm1)) {
      if (sm1 >= sm) {
        long long int tt = QryR(pos);
        mn = min(mn, tt);
        pos += mn;
      } else {
        long long int tt = QryL(pos);
        mn = min(mn, tt);
        pos -= mn;
      }
      tme += mn;
    } else {
      tme = a[ln].t;
    }
    if (tot == n) break;
  }
  for (int i = 0; i < n; i++) printf("%lld\n", ans[i]);
  return 0;
}
