#include<bits/stdc++.h>
using namespace std;
using ld = long double;
#define int long long
#define endl '\n'
#define all(x) x.begin(), x.end()

const int mod = 1e9 + 7;

template <class T> 
struct Point {
    typedef Point P;
    T x, y; 
    explicit Point(T x = 0, T y = 0) : x(x), y (y) {}
    bool operator==(P p) const {return tie(x, y) == tie(p.x, p.y); }
    bool operator<(P p) const {return tie(x, y) < tie(p.x, p.y); }
    P operator+(P p) const {return P(x+p.x, y+p.y);}
    P operator-(P p) const {return P(x-p.x, y-p.y);}
    T cross(P p) const {return x*p.y - y*p.x;}
    T dot(P p) const {return x*p.x + y*p.y;}
    T cross(P a, P b) const {return (a-*this).cross(b-*this);}
    T dist2() {return x*x + y*y; }
};

using P = Point<int>;

bool half(P p) {
    return p.y < 0 || (p.y == 0 && p.x < 0);
}

struct FT {
    int n; vector<int> s; 
    FT(int n) : n(n+1) , s(n+1) {}
    void update(int pos, int dif) {
        for(; pos < n; pos += pos & -pos) s[pos] += dif;
    }
    int query(int pos) {
        int sm = 0;
        for(; pos > 0; pos -= pos & -pos) sm += s[pos];
        return sm;
    }
    int query(int l, int r) {
        return query(l) - query(l-1);
    }
}; 

void polar_sort(vector<P> & v, P o = P(0,0)) {
    sort(v.begin()+1, v.end(), [&](P a, P b) {
        P u = a - o, w = b - o;
        return make_tuple(half(u), 0, u.dist2()) < make_tuple(half(w), u.cross(w), w.dist2());
    });
}

void solve() {
    int n, d; cin >> n >> d;     
    vector<int> w(n+1);

    map<P, int> ind;
    vector<P> pts(n+1);
    P cen1 = P(0,0), cen2 = P(d, 0);
    for(int i = 1; i <= n; i++) {
        cin >> pts[i].x >> pts[i].y >> w[i];
        ind[pts[i]] = i;
    }
    vector<int> aa(n+1), bb(n+1);
    polar_sort(pts, cen1);
    for(int i = 1; i <= n; i++) {
        aa[i] = ind[pts[i]];
    }
    polar_sort(pts, cen2);
    for(int i = 1; i <= n; i++) {
        bb[i] = ind[pts[i]];
    }
    reverse(bb.begin()+1, bb.end());

    // for(int i = 1; i <= n; i++) cout << aa[i] << ' ';
    // cout << '\n';
    // for(int i = 1; i <= n; i++) cout << bb[i]  << ' ';
    // cout << '\n';

    vector<int> nw(n+1);
    vector<int> perm(n+1);
    for(int i = 1; i <= n; i++) {
        perm[aa[i]] = i;
        nw[i] = w[aa[i]];
    }
    for(int i = 1; i <= n; i++) {
        bb[i] = perm[bb[i]];
    }
    for(int i = 1; i <= n; i++) {
        cout << bb[i] << ' ';
    }
    cout << '\n';

    int ans = 0;
    FT ft(n+5);
    for(int i = n; i >= 1; i--) {
        ans += ft.query(1, bb[i]-1) * bb[i];
        ft.update(bb[i], nw[i]);
    }
    cout << ans << '\n';

}

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(nullptr);
    int tc = 1;
    // cin >> tc;
    for(int i = 1; i <= tc; i++) {
        solve();
    }
}