It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.

So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers l_{i}, r_{i}, cost_{i} — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value r_{i} - l_{i} + 1.

At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: r_{i} < l_{j} or r_{j} < l_{i}.

Help Leha to choose the necessary vouchers!


-----Input-----

The first line contains two integers n and x (2 ≤ n, x ≤ 2·10^5) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.

Each of the next n lines contains three integers l_{i}, r_{i} and cost_{i} (1 ≤ l_{i} ≤ r_{i} ≤ 2·10^5, 1 ≤ cost_{i} ≤ 10^9) — description of the voucher.


-----Output-----

Print a single integer — a minimal amount of money that Leha will spend, or print  - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.


-----Examples-----
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4

Output
5

Input
3 2
4 6 3
2 4 1
3 5 4

Output
-1



-----Note-----

In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.

In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2.

Difficulty: interview

Starter code:

Solutions:
import bisect
import collections


def solve(inp, *args):
    n, x = list(map(int, inp.split(" ", 1)))
    travels_by_len = collections.defaultdict(list)
    travels_by_len_processed = {}
    for travel in args:
        l, r, cost = list(map(int, travel.split(" ", 2)))
        travels_by_len[r - l + 1].append((l, r, cost))
    for travel_len, travels in list(travels_by_len.items()):
        travels.sort()
        travels_processed = [(travels[-1][0], travels[-1][2])]
        for i in range(len(travels) - 2, -1, -1):
            prev_travel = travels_processed[-1]
            l, r, c = travels[i]
            travels_processed.append((l, min(c, prev_travel[1])))
        travels_by_len_processed[travel_len] = travels_processed[::-1]

    best_price = float("inf")
    for first_travel_len, first_travels in list(travels_by_len.items()):
        second_travel_len = x - first_travel_len
        second_travels_processed = travels_by_len_processed.get(second_travel_len, [])
        for first_travel in first_travels:
            l1, r1, c1 = first_travel
            # now we look for cheapest travels which have l2 > r1
            idx = bisect.bisect_right(second_travels_processed, (r1, float("inf")))
            if 0 <= idx < len(second_travels_processed):
                best_price = min(best_price, c1 + second_travels_processed[idx][1])
    return -1 if best_price == float("inf") else best_price


def __starting_point():
    inp = input()
    n, x = list(map(int, inp.split(" ", 1)))
    print(solve(inp, *(input() for i in range(n))))

__starting_point()

import sys

def main():
    n,x = map(int,sys.stdin.readline().split())
    al = []
    starts = []
    finishes = []
    y = [-1] * 200002
    for i in range(n):
        a,b,c = map(int, sys.stdin.readline().split())
        al.append((a,b,c))
        starts.append((a,i))
        finishes.append((b,i))

    finishes = sorted(finishes, key=lambda x: x[0])
    starts = sorted(starts, key=lambda x: x[0])
    j =0
    
    res = 3*(10**9)
    for i in range(n):
        while j<n and starts[j][0] <= finishes[i][0]:
            c = starts[j][1]
            h = al[c][1] -al[c][0]+1
            cost = al[c][2]
            if y[x-h]!= -1 and y[x-h] + cost < res:
                res = y[x-h] + cost
            j+=1
        c = finishes[i][1]
        h = al[c][1] - al[c][0]+1
        cost = al[c][2]
        if y[h]== -1 or y[h] > cost:
            y[h] = cost

    if res == 3*(10**9):
        print(-1)
    else:
        print(res)


main()

from sys import stdin, stdout

n, x = map(int, stdin.readline().split())
length = [[] for i in range(x + 1)]
mins = [[] for i in range(x + 1)]
challengers = []

for i in range(n):
    l, r, v = map(int, stdin.readline().split())
    
    if (r - l < x):
        length[r - l + 1].append((l, v))

for i in range(1, x + 1):
    if not len(length[i]):
        continue
    else:
        length[i].sort()
        mn = length[i][-1][-1]
        mins[i].append(mn)
        
        for j in range(len(length[i]) - 2, -1, -1):
            mn = min(mn, length[i][j][-1])
            mins[i].append(mn)
        
        mins[i] = mins[i][::-1]


INF = float('inf')
ans = INF

for i in range(1, x + 1):
    if not len(length[i]) or not len(length[x - i]):
        continue
    
    for j in range(len(length[i])):
        pos, v = length[i][j]
        
        l, r = -1, len(length[x - i])
        while (r - l > 1):
            m = (r + l) // 2
            
            if length[x - i][m][0] > pos + i - 1:
                r = m
            else:
                l = m
                
        if r != len(length[x - i]):
            ans = min(ans, v + mins[x - i][r])


if ans == INF:
    stdout.write('-1')
else:
    stdout.write(str(ans))

import sys

def main():
    n,x = map(int,sys.stdin.readline().split())
    al = []
    starts = []
    finishes = []
    y = [-1] * 200002
    for i in range(n):
        a,b,c = map(int, sys.stdin.readline().split())
        al.append((a,b,c))
        starts.append((a,i))
        finishes.append((b,i))

    finishes = sorted(finishes, key=lambda x: x[0])
    starts = sorted(starts, key=lambda x: x[0])
    j =0
    
    res = 3*(10**9)
    for i in range(n):
        while j<n and starts[j][0] <= finishes[i][0]:
            c = starts[j][1]
            h = al[c][1] -al[c][0]+1
            cost = al[c][2]
            if y[x-h]!= -1 and y[x-h] + cost < res:
                res = y[x-h] + cost
            j+=1
        c = finishes[i][1]
        h = al[c][1] - al[c][0]+1
        cost = al[c][2]
        if y[h]== -1 or y[h] > cost:
            y[h] = cost

    if res == 3*(10**9):
        print(-1)
    else:
        print(res)


main()

import sys

#def main():
if 1==1:
    n,x = list(map(int,sys.stdin.readline().split()))
    al = []
    starts = []
    finishes = []
    y = [-1] * 200002
    for i in range(n):
        a,b,c = list(map(int, sys.stdin.readline().split()))
        al.append((a,b,c))
        starts.append((a,i))
        finishes.append((b,i))

    finishes = sorted(finishes, key=lambda x: x[0])
    starts = sorted(starts, key=lambda x: x[0])
    j =0
    
    res = 3*(10**9)
    for i in range(n):
        while j<n and starts[j][0] <= finishes[i][0]:
            c = starts[j][1]
            h = al[c][1] -al[c][0]+1
            cost = al[c][2]
            if y[x-h]!= -1 and y[x-h] + cost < res:
                res = y[x-h] + cost
            j+=1
        c = finishes[i][1]
        h = al[c][1] - al[c][0]+1
        cost = al[c][2]
        if y[h]== -1 or y[h] > cost:
            y[h] = cost

    if res == 3*(10**9):
        print(-1)
    else:
        print(res)


#main()


import sys

def main():
    n,x = map(int,sys.stdin.readline().split())
    al = []
    starts = []
    finishes = []
    y = [-1 for i in range(200002)]
    for i in range(n):
        a,b,c = map(int, sys.stdin.readline().split())
        al.append((a,b,c))
        starts.append((a,i))
        finishes.append((b,i))

    finishes = sorted(finishes, key=lambda x: x[0])
    starts = sorted(starts, key=lambda x: x[0])
    j =0
    
    res = 3*(10**9)
    for i in range(n):
        while j<n and starts[j][0] <= finishes[i][0]:
            c = starts[j][1]
            h = al[c][1] -al[c][0]+1
            cost = al[c][2]
            if y[x-h]!= -1 and y[x-h] + cost < res:
                res = y[x-h] + cost
            j+=1
        c = finishes[i][1]
        h = al[c][1] - al[c][0]+1
        cost = al[c][2]
        if y[h]== -1 or y[h] > cost:
            y[h] = cost

    if res == 3*(10**9):
        print(-1)
    else:
        print(res)


main()

n,x=list(map(int,input().split()))
V=[]
for i in range(x+1):
    V.append([])
for i in range(n):
    l,r,c=list(map(int,input().split()))
    if r-l+1>x:
        continue
    V[r-l+1].append((l,c))
for i in range(x+1):
    V[i]=sorted(V[i],key=lambda a:a[0])
ans=int(3e9+7)
for i in range(x+1):
    mn=int(3e9+7)
    k=0
    for j in range(len(V[i])):
        while k!=len(V[x-i]) and V[x-i][k][0]+(x-i)-1<V[i][j][0]:
            mn=min(mn,V[x-i][k][1])
            k+=1
        ans=min(ans,mn+V[i][j][1])
if ans==int(3e9+7):
    print(-1)
else:
    print(ans)


n,x=list(map(int,input().split()))
V=[]
for i in range(x+1):
    V.append([])
for i in range(n):
    l,r,c=list(map(int,input().split()))
    if r-l+1>x:
        continue
    V[r-l+1].append((l,c))
for i in range(x+1):
    V[i]=sorted(V[i],key=lambda a:a[0])
ans=int(3e9+7)
for i in range(x+1):
    mn=int(3e9+7)
    k=0
    for j in range(len(V[i])):
        while k!=len(V[x-i]) and V[x-i][k][0]+(x-i)-1<V[i][j][0]:
            mn=min(mn,V[x-i][k][1])
            k+=1
        ans=min(ans,mn+V[i][j][1])
if ans==int(3e9+7):
    print(-1)
else:
    print(ans)


n,x=list(map(int,input().split()))
V=[]
for i in range(x+1):
    V.append([])
for i in range(n):
    l,r,c=list(map(int,input().split()))
    if r-l+1<=x:
        V[r-l+1].append([l,r,c])
for i in range(x+1):
    V[i]=sorted(V[i])
ans=int(3e9+7)
for i in range(x+1):
    mn=int(3e9+7)
    p=0
    k=0
    for j in range(len(V[i])):
        while k!=len(V[x-i]) and V[i][j][0]>V[x-i][k][1] :
            mn=min(mn,V[x-i][k][2])
            k=k+1
        ans=min(ans,mn+V[i][j][2])
if ans==int(3e9+7):
    print(-1)
else:
    print(ans)


#created by BinZhaO
n,x=list(map(int,input().split()))
V=[]
for i in range(x+1):
    V.append([])
for i in range(n):
    l,r,c=list(map(int,input().split()))
    if r-l+1<=x:
        V[r-l+1].append([l,r,c])
for i in range(x+1):
    V[i]=sorted(V[i])
ans=int(3e9+7)
for i in range(x+1):
    mn=int(3e9+7)
    p=0
    k=0
    for j in range(len(V[i])):
        while k!=len(V[x-i]) and V[i][j][0]>V[x-i][k][1] :
            mn=min(mn,V[x-i][k][2])
            k=k+1
        ans=min(ans,mn+V[i][j][2])
if ans==int(3e9+7):
    print(-1)
else:
    print(ans)


def main():
    n, x = list(map(int, input().split()))
    vouchers = [False] * x
    for _ in range(n):
        lo, hi, cost = list(map(int, input().split()))
        w = hi - lo
        if w < x:
            l = vouchers[w]
            if l:
                l.append((lo, cost))
            else:
                vouchers[w] = [(lo, cost)]
    best = inf = 1 << 31
    x -= 2
    if not x & 1 and vouchers[x // 2]:
        lh, h = vouchers[x // 2], None
        for f in lh.sort, lh.reverse:
            l = h
            f()
            h, b = [], inf
            for lo, cost in lh:
                if b > cost:
                    b = cost
                    h.append((lo, b))
        b, v = h.pop()
        for a, u in l:
            a += w
            while a >= b and h:
                b, v = h.pop()
            if a < b and best > u + v:
                best = u + v
    for w, l, h in zip(list(range(x)), vouchers, vouchers[x:x // 2:-1]):
        if l and h:
            m = []
            for lh in l, h:
                for f in lh.sort, lh.reverse:
                    f()
                    t, b = [], inf
                    for lo, cost in lh:
                        if b > cost:
                            b = cost
                            t.append((lo, b))
                    m.append(t)
            for l, h in (m[0], m[3]), (m[2], m[1]):
                b, v = h.pop()
                for a, u in l:
                    a += w
                    while a >= b and h:
                        b, v = h.pop()
                    if a < b and best > u + v:
                        best = u + v
                w = x - w
    print(best if best < inf else -1)


def __starting_point():
    main()

__starting_point()

n,x=list(map(int,input().split()))
a=[]
for i in range(x+1):a.append([])
#делаем герлянду хешем по длинам (не берем не нужное)
for i in range(n):
    L,R,C=list(map(int,input().split()))
    if R-L+1>x:continue
    a[R-L+1].append([L,R,C])
#сортируем по левым границам
for i in range(x+1):a[i]=sorted(a[i])

ans=int(3e9+1)
for i in range(x+1):
    m=int(3e9+1)
    z=0
    for j in range(len(a[i])):
        while z!=len(a[x-i]) and a[i][j][0]>a[x-i][z][1]:
            m=min(m,a[x-i][z][2])
            z+=1
        ans=min(ans,m+a[i][j][2])
if ans==int(3e9+1):
    print(-1)
else:
    print(ans)


from math import inf

n, x = list(map(int, input().split()))

a = []
best = [inf for i in range(2 * (10**5) + 1)]
ans = inf

for i in range(n):
    l, r, cost = list(map(int, input().split()))

    a.append((l, cost, r-l+1, True))
    a.append((r, cost, r-l+1, False))

a.sort(key=lambda element: 10 * element[0] + 1 - int(element[3]))

for item in a:
    if item[3] and x > item[2]:
        ans = min(ans, item[1] + best[x - item[2]])

    if not item[3]:
        best[item[2]] = min(best[item[2]], item[1])


print(ans if ans != inf else -1)


n, x = map(int, input().split())
data = []
for i in range(x + 1):
    data.append([])
for i in range(n):
    l, r, c = map(int, input().split())
    if r - l + 1 <= x:
        data[r - l + 1].append([l, r, c])
for i in range(x + 1):
    data[i].sort()

answer = int(3e9+7)
for i in range(x + 1):
    k, b = 0, int(3e9+7)
    for j in range(len(data[i])):
        while k != len(data[x - i]) and data[i][j][0] > data[x - i][k][1]:
            b = min(b, data[x - i][k][2])
            k += 1
        answer = min(answer, b + data[i][j][2])
        

if answer == int(3e9+7):
    print(-1)
else:
    print(answer)

n, x = map(int, input().split())
data = []
for i in range(x + 1):
    data.append([])
for i in range(n):
    l, r, c = map(int, input().split())
    if r - l + 1 <= x:
        data[r - l + 1].append([l, r, c])
for i in range(x + 1):
    data[i].sort()

answer = 9 * 10 ** 12
for i in range(x + 1):
    k, b = 0, int(9 * 10 ** 12)
    for j in range(len(data[i])):
        while k != len(data[x - i]) and data[i][j][0] > data[x - i][k][1]:
            b = min(b, data[x - i][k][2])
            k += 1
        answer = min(answer, b + data[i][j][2])
        

if answer == int(9 * 10 ** 12):
    print(-1)
else:
    print(answer)

n,x=map(int,input().split())
V=[]
for i in range(x+1):
    V.append([])
for i in range(n):
    l,r,c=map(int,input().split())
    if r-l+1>x:
        continue
    V[r-l+1].append((l,c))
for i in range(x+1):
    V[i]=sorted(V[i],key=lambda a:a[0])
ans=int(3e9+7)
for i in range(x+1):
    mn=int(3e9+7)
    k=0
    for j in range(len(V[i])):
        while k!=len(V[x-i]) and V[x-i][k][0]+(x-i)-1<V[i][j][0]:
            mn=min(mn,V[x-i][k][1])
            k+=1
        ans=min(ans,mn+V[i][j][1])
if ans==int(3e9+7):
    print(-1)
else:
    print(ans)

(n, x) = [int(x) for x in input().split()]

l_ind = [[] for i in range(200000)]
r_ind = [[] for i in range(200000)]

for i in range(n):
    (l, r, c) = [int(x) for x in input().split()]
    if r - l + 1 <= x:
        l_ind[l - 1].append((l, r, c))
        r_ind[r - 1].append((l, r, c))


ans = None

bestCost = [None for i in range(x + 1)]

for (vl, vr) in zip(l_ind, r_ind):
    for (l, r, c) in vl:
        cur_len = r - l + 1
        if bestCost[x - cur_len]:
            if ans:
                ans = min(ans, c + bestCost[x - cur_len])
            else:
                ans = c + bestCost[x - cur_len]
    for (l, r, c) in vr:
        cur_len = r - l + 1
        if bestCost[cur_len]:
            bestCost[cur_len] = min(bestCost[cur_len], c)
        else:
            bestCost[cur_len] = c


if ans == None:
    print(-1)
else:
    print(ans)


n, x = map(int, input().split())
data = []
for i in range(x + 1):
    data.append([])
for i in range(n):
    l, r, c = map(int, input().split())
    if r - l + 1 <= x:
        data[r - l + 1].append([l, r, c])
for i in range(x + 1):
    data[i].sort()


answer = int(20000000000)
for i in range(x + 1):
    k, b = 0, int(20000000000)
    for j in range(len(data[i])):
        while k != len(data[x - i]) and( data[i][j][0] > data[x - i][k][1] ):
            b = min(b, data[x - i][k][2])
            k += 1
        answer = min(answer, b + data[i][j][2])

if answer == int(20000000000):
    print(-1)
else:
    print(answer)

n,x=map(int,input().split())
V=[]
for i in range(x+1):
    V.append([])
for i in range(n):
    l,r,c=map(int,input().split())
    if r-l+1>x:
        continue
    V[r-l+1].append((l,c))
for i in range(x+1):
    V[i]=sorted(V[i],key=lambda a:a[0])
ans=int(3e9+7)
for i in range(x+1):
    mn=int(3e9+7)
    k=0
    for j in range(len(V[i])):
        while k!=len(V[x-i]) and V[x-i][k][0]+(x-i)-1<V[i][j][0]:
            mn=min(mn,V[x-i][k][1])
            k+=1
        ans=min(ans,mn+V[i][j][1])
if ans==int(3e9+7):
    print(-1)
else:
    print(ans)

n,x=map(int,input().split())
V=[]
for i in range(x+1):
    V.append([])
for i in range(n):
    l,r,c=map(int,input().split())
    if r-l+1<=x:
        V[r-l+1].append([l,r,c])
for i in range(x+1):
    V[i]=sorted(V[i])
ans=int(3e9+7)
for i in range(x+1):
    mn=int(3e9+7)
    p=0
    k=0
    for j in range(len(V[i])):
        while k!=len(V[x-i]) and V[i][j][0]>V[x-i][k][1] :
            mn=min(mn,V[x-i][k][2])
            k=k+1
        ans=min(ans,mn+V[i][j][2])
if ans==int(3e9+7):
    print(-1)
else:
    print(ans)

from bisect import bisect_left
f = lambda: map(int, input().split())
n, x = f()
s, t = {}, {}
for i in range(n):
    l, r, c = f()
    d = r - l
    if d not in s: s[d] = []
    s[d].append((l, c))
for d, p in s.items():
    p.sort(key=lambda q: q[0])
    q = t[d] = [[l, c] for l, c in p]
    for i in range(1, len(q))[::-1]:
        q[i - 1][1] = min(q[i - 1][1], q[i][1])
m = 3e9
for d in s:
    p = t.get(x - 2 - d, [])
    if p:
        for l, c in s[d]:
            i = bisect_left(p, [l + d + 1, 0])
            if i < len(p): m = min(m, c + p[i][1])
print(-1 if m == 3e9 else m)

f = lambda: map(int, input().split())
n, x = f()
s = [[] for i in range(x - 1)]
for d in range(n):
    l, r, c = f()
    if r - l < x - 1: s[r - l].append((l, c))
for t in s: t.sort(key=lambda q: q[0])
m = 3e9
for d, t in enumerate(s):
    D = x - 2 - d
    i, T = 0, s[D]
    M = 3e9
    for l, c in t:
        while i < len(T) and l > T[i][0] + D:
            M = min(M, T[i][1])
            i += 1
        m = min(m, c + M)
print(-1 if m == 3e9 else m)

f = lambda: list(map(int, input().split()))
n, x = f()
s = [[] for i in range(x - 1)]
for d in range(n):
    l, r, c = f()
    if r - l < x - 1: s[r - l].append((l, c))
for t in s: t.sort(key=lambda q: q[0])
m = 3e9
for d, t in enumerate(s):
    D = x - 2 - d
    i, T = 0, s[D]
    M = 3e9
    for l, c in t:
        while i < len(T) and l > T[i][0] + D:
            M = min(M, T[i][1])
            i += 1
        m = min(m, c + M)
print(-1 if m == 3e9 else m)


f = lambda: list(map(int, input().split()))
n, x = f()
s = [[] for i in range(x - 1)]
for d in range(n):
    l, r, c = f()
    if r - l < x - 1: s[r - l] += [[l,c]]
for t in s: t.sort(key=lambda q: q[0])
m = 3e9
for d, t in enumerate(s):
    D = x - 2 - d
    i, T = 0, s[D]
    M = 3e9
    for l, c in t:
        while i < len(T) and l > T[i][0] + D:
            M = min(M, T[i][1])
            i += 1
        m = min(m, c + M)
print(-1 if m == 3e9 else m)


f = lambda: list(map(int, input().split()))
n, x = f()
s = [[] for i in range(x - 1)]
for d in range(n):
    l, r, c = f()
    if r - l < x - 1: s[r - l].append((l, c))
for t in s: t.sort(key=lambda q: q[0])
m = 3e9
for d, t in enumerate(s):
    D = x - 2 - d
    i, T = 0, s[D]
    M = 3e9
    for l, c in t:
        while i < len(T) and l > T[i][0] + D:
            M = min(M, T[i][1])
            i += 1
        m = min(m, c + M)
print(-1 if m == 3e9 else m)




# Made By Mostafa_Khaled