#include <bits/stdc++.h>
using namespace std;
struct node {
  long long a, b, c;
  long long index;
  bool operator<(const node &cur) const { return c < cur.c; }
};
struct node task[55];
long long dp[55][55][105];
long long path[55][55][105][2];
long long n, m, p;
int main() {
  long long i, j, k, l;
  while (scanf("%I64d%I64d%I64d", &n, &m, &p) != EOF) {
    for (i = 1; i <= m; i++) {
      scanf("%I64d%I64d%I64d", &task[i].a, &task[i].b, &task[i].c);
      task[i].index = i;
    }
    sort(task + 1, task + 1 + m);
    memset(dp, 0, sizeof(dp));
    memset(path, 0, sizeof(path));
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        for (k = 0; k <= task[j].b - task[j].a; k++) {
          if (i == 1) {
            if (dp[i][j][k] < task[j].a + k) {
              dp[i][j][k] = task[j].a + k;
              path[i][j][k][0] = 0;
              path[i][j][k][1] = 0;
            }
          } else {
            long long need = 0, ck;
            if ((task[j].a + k) % p == 0) need = 1;
            for (l = 1; l <= j - 1; l++) {
              if (task[l].c == task[j].c) continue;
              ck = (task[j].a + k) / p;
              if (need && ck >= task[l].a && ck <= task[l].b) {
                if (dp[i - 1][l][ck - task[l].a]) {
                  if (dp[i][j][k] <
                      dp[i - 1][l][ck - task[l].a] + k + task[j].a) {
                    dp[i][j][k] = dp[i - 1][l][ck - task[l].a] + k + task[j].a;
                    path[i][j][k][0] = l;
                    path[i][j][k][1] = ck - task[l].a;
                  }
                }
              }
              ck = task[j].a + k - p;
              if (ck >= task[l].a && ck <= task[l].b) {
                if (dp[i - 1][l][ck - task[l].a]) {
                  if (dp[i][j][k] <
                      dp[i - 1][l][ck - task[l].a] + k + task[j].a) {
                    dp[i][j][k] = dp[i - 1][l][ck - task[l].a] + k + task[j].a;
                    path[i][j][k][0] = l;
                    path[i][j][k][1] = ck - task[l].a;
                  }
                }
              }
            }
          }
        }
      }
    }
    long long ans = 0;
    long long res[55][2], next[2];
    for (j = 1; j <= m; j++) {
      for (k = 0; k <= task[j].b - task[j].a; k++) {
        if (ans < dp[n][j][k]) {
          ans = dp[n][j][k];
          res[n][0] = task[j].index;
          res[n][1] = k + task[j].a;
          next[0] = path[n][j][k][0];
          next[1] = path[n][j][k][1];
        }
      }
    }
    if (ans > 0) {
      printf("YES\n", ans);
      for (i = n - 1; i >= 1; i--) {
        long long pj = next[0], pk = next[1];
        res[i][0] = task[pj].index;
        res[i][1] = task[pj].a + pk;
        next[0] = path[i][pj][pk][0];
        next[1] = path[i][pj][pk][1];
      }
      for (i = 1; i <= n; i++) {
        printf("%I64d %I64d\n", res[i][0], res[i][1]);
      }
    } else {
      printf("NO\n");
    }
  }
  return 0;
}
