#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,d,t1,t2,d1,d2,num1,num2;
cin >> t >> d >> t1 >> d1 >> t2 >> d2;
if(d1<d&&d2<d){
cout << "-1";
return 0;
}else {
if(t1>t){
num1=t1-t;
}else{
num1=0;
}
if(t2>t){
num2=t2-t;
}else{
num2=0;
}
}cout << min(num1,num2);
return 0;
}