((y,z):l)&(w,x)|x*y<1=(w+y,x+z):l
(q:l)&p=p:q:l
(p@(u,v):r@(y,z):l)%q@(w,x)=[((y-w,z):l)&q&(u,v-x)|w<=y,x<=v]++[p:m|m<-(r:l)%q]
_%_=[]
g m
(p:n
)l
=any(g
[]$m
++n
)(l
%p
)||g
(p:m
)n l
g[]_[_,_,_]=0<1
g _[]_=0<0
f=($[(0,9^9),(9^9,0)]).g[]
main = do
print (f
[(1,1),(1,5),(2,6)]) print (f
[(1,1),(1,2),(1,2),(2,1),(2,1)]) print (f
[(3,2),(4,1),(2,1),(4,1),(2,1),(5,2),(3,2),(1,4),(3,2),(2,1),(2,1),(1,1),(5,1)])
KCh5LHopOmwpJih3LHgpfHgqeTwxPSh3K3kseCt6KTpsCihxOmwpJnA9cDpxOmwKKHBAKHUsdik6ckAoeSx6KTpsKSVxQCh3LHgpPVsoKHktdyx6KTpsKSZxJih1LHYteCl8dzw9eSx4PD12XSsrW3A6bXxtPC0ocjpsKSVxXQpfJV89W10KZyBtKHA6bilsPWFueShnW10kbSsrbikobCVwKXx8ZyhwOm0pbiBsCmdbXV9bXyxfLF9dPTA8MQpnIF9bXV89MDwwCmY9KCRbKDAsOV45KSwoOV45LDApXSkuZ1tdCgptYWluID0gZG8KICBwcmludCAoZiBbKDEsMSksKDEsNSksKDIsNildKQogIHByaW50IChmIFsoMSwxKSwoMiwyKV0pCiAgcHJpbnQgKGYgWygxLDEpLCgxLDIpLCgxLDIpLCgyLDEpLCgyLDEpXSkKICBwcmludCAoZiBbKDMsMiksKDQsMSksKDIsMSksKDQsMSksKDIsMSksKDUsMiksKDMsMiksKDEsNCksKDMsMiksKDIsMSksKDIsMSksKDEsMSksKDUsMSldKQ==