local n,ma,mb=io.read("n","n","n")
local n1=n//2
local n2=n//2+n%2
local a1,b1,c1={},{},{}
local a2,b2,c2={},{},{}
for i=1,n1 do
    a1[i],b1[i],c1[i]=io.read("n","n","n")
end
for i=1,n2 do
    a2[i],b2[i],c2[i]=io.read("n","n","n")
end

local map1={}
for bit=0,(1<<n1) do
    local asum=0
    local bsum=0
    local csum=0
    for i=1,n1 do
        if bit&1<<(i-1)>0 then
            asum=asum+a1[i]
            bsum=bsum+b1[i]
            csum=csum+c1[i]
        end
    end
    if csum>0 then
        map1[asum]=map1[asum] or {}
        if not map1[asum][bsum] then
            map1[asum][bsum]=csum
        else
            map1[asum][bsum]=math.min(map1[asum][bsum],csum)
        end
    end
end
local map2={}
for bit=0,(1<<n2) do
    local asum=0
    local bsum=0
    local csum=0
    for i=1,n2 do
        if bit&1<<(i-1)>0 then
            asum=asum+a2[i]
            bsum=bsum+b2[i]
            csum=csum+c2[i]
        end
    end
    if csum>0 then
        map2[asum]=map2[asum] or {}
        if not map2[asum][bsum] then
            map2[asum][bsum]=csum
        else
            map2[asum][bsum]=math.min(map2[asum][bsum],csum)
        end
    end
end

local m=math.max(ma,mb)
local INF=10^13
local mincost=INF
for a1,_ in pairs(map1) do
    for b1,c1 in pairs(map1[a1]) do
        for i=0,400,m do
            i=i//m
            local c2=(map2[i*ma-a1] and map2[i*ma-a1][i*mb-b1] or INF)
            mincost=math.min(mincost,c1+c2)
        end
    end
end
print(INF>mincost and mincost or -1)