#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int End,n;
struct node
{
int value;
long long time;
friend bool operator<(const node &u,const node &v)
{
if(u.time==v.time)
return u.value<v.value;
return u.time>v.time;
}
};
struct data
{
int a,b,c;
friend bool operator<(const data &u,const data &v)
{
return u.b<v.b;
}
}num[100010];
bool mk[100010];
void DFS()
{
memset(mk,0,sizeof(mk));
node start;
start.value=1;
start.time=0;
int st=0;
priority_queue<node>q;
q.push(start);
while(!q.empty())
{
node now = q.top();
q.pop();
if(now.value>=End)
{
printf("%lld\n",now.time);
return ;
}
int k;
for( k=st;k<n&&now.value>=num[k].b;k++)
{
if(mk[k]||now.value==num[k].a)
continue;
mk[k]=true;
node temp = now;
temp.value=num[k].a;
temp.time+=num[k].c;
q.push(temp);
}
st=k;
}
printf("-1\n");
}
int main()
{
int t,i;
scanf("%d",&t);
int cnt=1;
while (t--)
{
scanf("%d%d",&n,&End);
int Max=-1;
for(i=0;i<n;i++)
{
scanf("%d%d%d",&num[i].a,&num[i].b,&num[i].c);
if(num[i].a>Max)
Max=num[i].a;
}
if(Max<End)
{
printf("-1\n");
continue;
}
sort(num,num+n);
printf("Case #%d: ",cnt++);
DFS();
}
return 0;
}
#include <queue>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int End,n;
struct node
{
int value;
long long time;
friend bool operator<(const node &u,const node &v)
{
if(u.time==v.time)
return u.value<v.value;
return u.time>v.time;
}
};
struct data
{
int a,b,c;
friend bool operator<(const data &u,const data &v)
{
return u.b<v.b;
}
}num[100010];
bool mk[100010];
void DFS()
{
memset(mk,0,sizeof(mk));
node start;
start.value=1;
start.time=0;
int st=0;
priority_queue<node>q;
q.push(start);
while(!q.empty())
{
node now = q.top();
q.pop();
if(now.value>=End)
{
printf("%lld\n",now.time);
return ;
}
int k;
for( k=st;k<n&&now.value>=num[k].b;k++)
{
if(mk[k]||now.value==num[k].a)
continue;
mk[k]=true;
node temp = now;
temp.value=num[k].a;
temp.time+=num[k].c;
q.push(temp);
}
st=k;
}
printf("-1\n");
}
int main()
{
int t,i;
scanf("%d",&t);
int cnt=1;
while (t--)
{
scanf("%d%d",&n,&End);
int Max=-1;
for(i=0;i<n;i++)
{
scanf("%d%d%d",&num[i].a,&num[i].b,&num[i].c);
if(num[i].a>Max)
Max=num[i].a;
}
if(Max<End)
{
printf("-1\n");
continue;
}
sort(num,num+n);
printf("Case #%d: ",cnt++);
DFS();
}
return 0;
}