#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline string toString(T a) {
  ostringstream os("");
  os << a;
  return os.str();
}
template <typename T>
inline long long toLong(T a) {
  long long res;
  istringstream os(a);
  os >> res;
  return res;
}
template <typename T>
inline T S(T a) {
  return a * a;
}
template <typename T>
inline T gcd(T a, T b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <typename T>
inline unsigned long long bigmod(T a, T b, T m) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0)
    return S(bigmod(a, b / 2, m)) % m;
  else
    return (a % m * bigmod(a, b - 1, m)) % m;
}
template <typename T>
inline vector<string> parse(T str) {
  vector<string> res;
  string s;
  istringstream os(str);
  while (os >> s) res.push_back(s);
  return res;
}
template <typename T>
inline unsigned long long dist(T A, T B) {
  unsigned long long res =
      (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y);
  return res;
}
template <typename T>
inline double cosAngle(T a, T b, T c) {
  double res = a * a + b * b - c * c;
  res = res / (2 * a * b);
  res = acos(res);
  return res;
}
template <typename T>
inline T power(T base, int po) {
  T res = 1;
  if (base == 0) return 0;
  for (int i = (0); i < (po); i++) res *= base;
  return res;
}
template <typename T>
inline bool isOn(T mask, T pos) {
  return mask & (1 << pos);
}
template <typename T>
inline int Off(T mask, T pos) {
  return mask ^ (1 << pos);
}
template <typename T>
inline int On(T mask, T pos) {
  return mask | (1 << pos);
}
struct Z {
  long long start, end;
  long long tim;
  int idx;
  void print() { cout << start << " " << end << " " << idx << endl; }
};
bool operator<(Z A, Z B) { return A.tim < B.tim; }
vector<Z> v[2][200005];
vector<Z> person;
int tree[2][200005];
long long res[200005];
int M;
void update(int id, int x, int val) {
  while (x <= 100500) {
    tree[id][x] += val;
    x += (x & -x);
  }
  return;
}
int query(int id, int x) {
  if (x == 0) return 0;
  int sum = 0;
  while (x > 0) {
    sum += tree[id][x];
    x -= (x & -x);
  }
  return sum;
}
int find1(int pos) {
  int low, high, mid, best = -1, a, b;
  low = 1, high = pos - 1;
  while (low <= high) {
    mid = (low + high) / 2;
    a = query(0, pos) - query(0, mid - 1);
    b = query(1, pos) - query(1, mid - 1);
    if (a + b) {
      best = mid;
      low = mid + 1;
    } else
      high = mid - 1;
  }
  return best;
}
int find2(int pos) {
  int low = pos + 1, high = M, mid, best = -1, a, b;
  while (low <= high) {
    mid = (low + high) / 2;
    a = query(0, mid) - query(0, pos);
    b = query(1, mid) - query(1, pos);
    if (a + b) {
      best = mid;
      high = mid - 1;
    } else
      low = mid + 1;
  }
  return best;
}
int main() {
  int i, j, k, x, y;
  int ks, cas;
  int n, m;
  cin >> n >> m;
  M = m;
  Z p;
  for (int i = (1); i < (n + 1); i++) {
    scanf("%d%d%d", &k, &x, &y);
    p.start = x;
    p.end = y;
    p.tim = k;
    p.idx = i;
    person.push_back(p);
  }
  sort((person).begin(), (person).end());
  long long a, b, c, t = 0, pos = 1;
  bool state = 1;
  i = 0;
  int up, down, dir;
  while (state == 1) {
    while (i < n && person[i].tim <= t) {
      p = person[i];
      update(0, p.start, 1);
      v[0][p.start].push_back(person[i]);
      i++;
    }
    for (j = 0; j < v[0][pos].size(); j++) {
      p = v[0][pos][j];
      v[1][p.end].push_back(p);
      update(0, p.start, -1);
      update(1, p.end, 1);
    }
    v[0][pos].clear();
    for (j = 0; j < v[1][pos].size(); j++) {
      p = v[1][pos][j];
      update(1, p.end, -1);
      res[p.idx] = t;
    }
    v[1][pos].clear();
    down = query(0, pos) + query(1, pos);
    up = query(0, 100001) + query(1, 100001) - down;
    if (up >= down)
      dir = 1;
    else
      dir = 0;
    int dis = 0;
    if (dir == 0) {
      long long x, y;
      x = find1(pos);
      dis = (pos - x);
      y = 1000000;
      y *= y;
      if (i < n) {
        y = person[i].tim - t;
      }
      dis = (dis > y ? y : dis);
      pos -= dis;
      t += dis;
    } else {
      long long x, y;
      x = find2(pos);
      if (x == -1) {
        if (i == n)
          state = 0;
        else
          t = person[i].tim;
      } else {
        dis = (x - pos);
        y = 1000000;
        y *= y;
        if (i < n) {
          y = person[i].tim - t;
        }
        dis = (dis > y ? y : dis);
        pos += dis;
        t += dis;
      }
    }
  }
  for (i = 1; i <= n; i++) {
    printf("%I64d\n", res[i]);
  }
}
