#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
//#include "MyMath.h"
//#include "MyDisjointset.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
#include <stdio.h>
using namespace std;
typedef pair<int, int> P;
const int INF = 1000000000;
struct edge {
	int from, to, cost;
};
int main() {
	int v, e, r; cin >> v >> e >> r;
	vector <edge> ed;
	for (int i = 0; i < e; i++) {
		int s, t, co; cin >> s >> t >> co;
		edge e1; e1.from = s; e1.to = t; e1.cost = co;
		ed.push_back(e1);
	}
	int *d = new int[v];
	fill(d, d + v, INF);
	d[r] = 0;
	while (true) {
		bool  update = false;
		for (int i = 0; i < ed.size(); i++) {
			edge ex = ed[i];
			if (d[ed[i].from] != INF && d[ed[i].to] > d[ed[i].from] + ex.cost) {
				d[ed[i].to] = d[ed[i].from] + ex.cost;
				//cout << "from " << d[ed[i].from] << " to " << d[ed[i].to] << " cost " << ex.cost << endl;
				update = true;
			}
		}
		if (!update)  break; 
	}
	for (int i = 0; i < v; i++) {
		if (d[i] == INF) cout << "INF" << endl;
		else  cout << d[i] << endl;
	}
	delete[] d;
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100005;
struct sj{
	int a,l,r;ll x;
	inline sj(){}
	inline sj(int a,int l,int r,ll x):a(a),l(l),r(r),x(x){}
	inline bool operator<(const sj &t)const{return x>t.x;}
}d[N];
inline void cmax(ll &a,ll b){if(a<b)a=b;}
inline ll Y(int i,ll k){
	ll a=d[i].a;
	if(k<=a)return k*d[i].l;
	else    return k*d[i].r+a*(d[i].l-d[i].r);
}
int main(){
	int n,i,a,l,r,x;
	ll L=0ll,R,M,S,g=0ll,t1,t2;
	scanf("%d%d",&n,&x);R=(S=(t1=((ll)x))*n)-1ll;
	for(i=1;i<=n;++i){
		scanf("%d%d%d",&a,&l,&r);t2=a;
		d[i]=sj(a,l,r,t1*r+t2*(l-r));
		g+=l*t2;
	}
	sort(d+1,d+n+1);
	while(L<=R){
		M=(L+R)>>1;
		l=M/x;
		r=M%x;
//		printf("M=%lld l=%d r=%d",M,l,r);
//		for(i=1;i<=n;++i)printf(" (%d,%d)",d[i].x,Y(i,r));
//		printf("\n");
		t1=-1000000000000ll;t2=0ll;
		for(i=1;i<=l;++i){
			t2+=d[i].x;
			cmax(t1,Y(i,r)-d[i].x);
		}
		t1+=d[i].x;
		for(   ;i<=n;++i)cmax(t1,Y(i,r));
		if(t1+t2>=g)R=(S=M)-1ll;else L=M+1ll;
	}
	printf("%lld",S);
	return 0;
}