#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include using namespace std; long long int a[3]; long long int tmp[3]; long long int d; int calculate_distance(int a, int b){ if (a > b){ swap(a, b); } swap(a, b); int dd = a - b; dd /= d; dd++; return dd; } long long int solve_min(){ if (a[0] == 0 || a[2] == 0){ return -1LL; } if (a[0] == a[1] || a[1] == a[2]){ return -1LL; } bool update = true; while (update){ update = false; if (a[0] < a[1]){ int k = a[1] - a[0]; if (k <= d || k%d == 0){ return -1LL; } } if (a[2] < a[1]){ int k = a[1] - a[2]; if (k <= d || k%d == 0){ return -1LL; } } return calculate_distance(min(a[0], a[2]), a[1]); if (a[0] < a[1] && a[2] < a[1]){ int countt = 0; int ta; int tb; if (a[0] > a[2]){ int tep = a[0]-a[2]; tep /= d; if (tep*d < a[0] - a[2]){ tep++; } ta = max(static_cast(a[0]-tep*d),0); tb = a[2]; countt += tep; } else{ int tep = a[2] - a[0]; tep /= d; if (tep*d < a[2] - a[0]){ tep++; } tb = max(static_cast(a[2]-tep*d),0); ta = a[0]; countt += tep; } if (ta == tb){ countt += calculate_distance(ta, a[1]); } else{ countt += calculate_distance(ta, a[1]); countt += calculate_distance(tb, a[1]); } return countt; } else{ if (a[0] < a[1]){ return calculate_distance(a[0], a[1]); } else{ if (a[2] < a[1]){ return calculate_distance(a[2], a[1]); } else{ return 0LL; } } } } } long long int solve_max(){ if (a[0] == a[1] || a[1] == a[2]){ return -1LL; } if (a[0] > a[1]){ if ((a[0] - a[1]) % d == 0){ return -1LL; } } if (a[2] > a[1]){ if ((a[2] - a[1]) % d == 0){ return -1LL; } } if (a[0] < a[2]){ swap(a[0], a[2]); } if (a[0] > a[1] && a[2] > a[1]){ int countt = 0; int ta; int tb; int dist = a[0] - a[2]; if (dist%d){ return calculate_distance(max(a[0],a[2]),a[1]); } else{ return calculate_distance(a[0], a[1]) + calculate_distance(a[0], a[2]); } } else{ if (a[0] > a[1]){ return calculate_distance(a[0], a[1]); } else{ if (a[2] > a[1]){ return calculate_distance(a[1], a[2]); } else{ return 0LL; } } } } long long int solvemin(){ long long int k = min(a[0], a[2]); if (k > a[1]){ return 0; } else{ if (k <= 0){ return -1; } if (d==0LL){ return -1LL; } return calculate_distance(a[1], k); } } long long int solvemax(){ long long int countt = 0; if (a[1] == 0){ return -1LL; } if (a[0] >= a[1]){ if (d==0LL){ return -1LL; } countt += calculate_distance(a[0], a[1]); } if (a[2] >= a[1]){ if (d==0LL){ return -1LL; } countt += calculate_distance(a[1], a[2]); } return countt; } int main(){ scanf("%lld", &d); for (int i = 0; i < 3; i++){ scanf("%lld", &a[i]); } long long int ans = solvemin(); if (ans != -1LL){ ans = min(ans, solvemax()); } else{ ans = solvemax(); return -1; } printf("%lld\n", ans); return 0; }