using System; using System.Collections.Generic; using System.Text; using System.Linq; class Program { public void Proc() { Reader.IsDebug = false; long minus = long.Parse(Reader.ReadLine()); long take1 = long.Parse(Reader.ReadLine()); long take2 = long.Parse(Reader.ReadLine()); long take3 = long.Parse(Reader.ReadLine()); long ans = -1; // 真ん中を減らす場合 long ret = this.GetAns1(take1, take2, take3, minus); if(ret >= 0) { ans = ret; } ret = this.GetAns2(take1, take2, take3, minus); if(ret >= 0) { if(ans == -1) { ans = ret; } else { ans = Math.Min(ans, ret); } } Console.WriteLine(ans); } private long GetAns2(long take1, long take2, long take3, long minus) { // 真ん中が引っ込む場合 long tmp1 = take1; long tmp2 = take2; long tmp3 = take3; long ret = 0; if(minus == 0) { return -1; } if(tmp1 == tmp3) { if(tmp1 == 0) { return -1; } tmp1 = Math.Max(0, tmp1 - minus); ret++; } long times = (tmp2 - Math.Min(tmp1,tmp3)) / minus + 1; tmp2 = Math.Max(0, tmp2 - times * minus); ret += times; if(tmp2 == tmp1 || tmp2 == tmp3) { if(tmp2 == 0) { return -1; } else { tmp2 = Math.Max(0, tmp2 - minus); ret++; } } return ret; } private long GetAns1(long take1, long take2, long take3, long minus) { // 最初から揃っている場合 if(take1 != take2 && take2 != take3 && take3 != take1) { if(take2 > Math.Max(take1, take3)) { return 0; } if(take2 < Math.Min(take1, take3)) { return 0; } } if(minus == 0) { return -1; } // 真ん中が出る場合 long tmp1 = take1; long tmp2 = take2; long tmp3 = take3; long ret = 0; if(tmp1 >= tmp2) { long times = (take1 - take2) / minus + 1; ret += times; tmp1 = Math.Max(0, tmp1 - (times * minus)); } if(tmp3 >= tmp2) { long times = (take3 - take2) / minus + 1; ret += times; tmp3 = Math.Max(0, tmp3 - (times * minus)); } if(tmp1 == tmp3) { if(tmp1 == 0) { return -1; } else { ret += 1; } } return ret; } public class Reader { public static bool IsDebug = true; private static String PlainInput = @" 0 0 1 2 "; private static System.IO.StringReader Sr = null; public static string ReadLine() { if (IsDebug) { if (Sr == null) { Sr = new System.IO.StringReader(PlainInput.Trim()); } return Sr.ReadLine(); } else { return Console.ReadLine(); } } public static int[] GetInt(char delimiter = ' ', bool trim = false) { string inptStr = ReadLine(); if (trim) { inptStr = inptStr.Trim(); } string[] inpt = inptStr.Split(delimiter); int[] ret = new int[inpt.Length]; for (int i = 0; i < inpt.Length; i++) { ret[i] = int.Parse(inpt[i]); } return ret; } } static void Main() { Program prg = new Program(); prg.Proc(); } }