#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second

const int mx=100010;
const ll mod=1e9+7;

vector<int> edges[2100];

struct UnionFind {  
  vector<int> par,sizes;                     // par[i]:iの親の番号(ex)par[3]=2 : 3の親が2
  UnionFind(int n) : par(n),sizes(n,1)  {    //最初は全てが根であるとして初期化
    for(int i=0; i<n; i++){ par[i]=i; }  
  }
  int root(int x) {                       //xが属する木の根を再帰で得る:root(x)={xの木の根}
    if(par[x]==x){ return x; }
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {                 // xとyの木を併合
    int rx = root(x);                        //xの根をrx
    int ry = root(y);                        //yの根をry
    if(rx == ry){ return; }                  //xとyの根が同じ(=同じ木にある)時はそのまま
    if(sizes[rx]<sizes[ry]){ swap(rx, ry); } //xの木をおおきくする
    par[ry] = rx; 
    sizes[rx] += sizes[ry];                  //sizes[ry]はもう使わない
  }
 
  bool check(int x, int y) {                 // x,yが属する木が同じならtrueを返す
    return root(x) == root(y);
  }
  
  int size(int x){                           //xが含まれる木の大きさを返す
    return sizes[root(x)];
  }
};

bool dfs(int p, set<int> &a){
  for(auto to:edges[p]){
    if(a.find(to)!=a.end()){ return true; }
    a.insert(to);
    if(dfs(to,a)){ return true; } else { a.erase(to); }
  }
  return false;
}
 
int main(){
  int n,m;
  cin >> n >> m;
  UnionFind uf(n);
  vector<P> aded;
  
  for(int i=0; i<m; i++){
    int x,y; cin >> x >> y; x--; y--;
    if(uf.check(x,y)){
      aded.emplace_back(x,y);
    } else {
      uf.unite(x,y);
      edges[x].emplace_back(y);
    }
  }
  set<int> shortest;
  int cnt=inf;
  for(auto tmp:aded){
    set<int> ans;
    ans.insert(tmp.F);
    ans.insert(tmp.S);
    if(dfs(tmp.S,ans)){
      if(ans.size()<cnt){
        cnt=ans.size();
        shortest = ans;
      }
    }
  }
  if(cnt!=inf){
      cout << shortest.size() << endl;
      for(auto anno:shortest){cout << anno+1 << endl;}
      return 0;
  }
  cout << -1 << endl;
  return 0;
}

#include<bits/stdc++.h>
#define MN 100000
#define ll long long 
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
ll res,v[MN+5],V[MN+5],ans,g[MN+5],G[MN+5];
int n,X,L[MN+5],R[MN+5],b[MN+5],rk[MN+5];
bool cmp(int x,int y){return v[x]+g[x]>v[y]+g[y];}
ll GetV(ll*s,int l,int r,int x){return s[r]-s[l-1]-((x>=l&&x<=r)?s[x]-s[x-1]:0);}
inline int Div(ll x,int y){return (x+y-1)/y;}
int main()
{
    n=read();X=read();
    for(int i=1;i<=n;++i)
    {
        b[i]=read();L[i]=read();R[i]=read();
        v[i]=1LL*(X-b[i])*R[i];g[i]=1LL*b[i]*L[i];
        rk[i]=i;ans+=b[i];
    }
    sort(rk+1,rk+n+1,cmp);
    for(int i=1;i<=n;++i) V[i]=V[i-1]+v[rk[i]],G[i]=G[i-1]+g[rk[i]];
    for(int i=1;i<=n;++i)
    {
        int l=0,r=n-1,mid,res=n;
        while(l<=r)
        {
            mid=l+r>>1;
            ll Val=GetV(V,1,mid,i)-GetV(G,mid+1,n,i)+v[rk[i]];
            if(Val>=0) res=mid,r=mid-1; else l=mid+1;
        }
        ll Val=GetV(V,1,res,i)-GetV(G,res+1,n,i);
        if(Val>=0) ans=min(ans,1LL*(res-(res>=i))*X+max(0,Div(g[rk[i]]-Val,L[rk[i]])));
        else ans=min(ans,1LL*(res-(res>=i))*X+b[rk[i]]+Div(-Val,R[rk[i]]));
    }
    cout<<ans;
	return 0;
}