using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            int a = input[0];
            int b = input[1];
            int c = input[2];
            int n = input[3];
            if ((a < c) || (b < c) || (a + b - c < 0) || (c > n - 1) || (a + b - c > n - 1))
            {
                Console.WriteLine("-1");
            }
            else
            {
                int result = n - (a + b - c);
                if (result >= 1)
                {
                    Console.WriteLine(result);
                }
                else
                {
                    Console.WriteLine("-1");
                }
            }
        }
    }
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){

		if(A < C || B < C){
			Console.WriteLine(-1);
			return;
		}
		if(A + B < C){
			Console.WriteLine(-1);
			return;
		}

		int n = A + B - C;
		if(N > n){

			Console.WriteLine(N - n);
		} else {
			Console.WriteLine(-1);
		}

	}
	int A, B, C, N;
	public Sol(){
		var d = ria();
		A = d[0]; B = d[1]; C = d[2]; N = d[3];
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}

using System;

namespace CF_991_A
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int A = int.Parse(s[0]);
            int B = int.Parse(s[1]);
            int C = int.Parse(s[2]);
            int N = int.Parse(s[3]);

            // if(A>N || B>N || C>N) Недостаточно, дает ошибку на 5 тесте

            if (A > N || B > N || C > N ||
                C > A || C > B)
            {
                Console.WriteLine(-1);
                return;
            }

            int k = N - (A + B - C);
            if (k <= 0)
                Console.WriteLine(-1);
            else
                Console.WriteLine(k);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1 {
	class program {
		static void Main(string[] args) {
			string str = Console.ReadLine();//从控制台读入输入
			string[] nums = str.Split(new string[]{ " " }, StringSplitOptions.None);//这里是以空格隔开
			int a = Convert.ToInt32(nums[0]);
			int b = Convert.ToInt32(nums[1]);
			int c = Convert.ToInt32(nums[2]);
			int d = Convert.ToInt32(nums[3]);
			int cnt = a-c+b;
			if (d-cnt<=0||a+b<c||a<c||b<c) {
				Console.WriteLine(-1);
			}
			else Console.WriteLine(d-cnt);
		}
	}
}

// Problem: 991A - If at first you don't succeed...
// Author: Gusztav Szmolik

using System;

class IfAtFirstYouDontSucceed
    {
    static int Main ()
        {
        string[] words = ReadSplitLine (4);
        if (words == null)
            return -1;
        byte a;
        if (!Byte.TryParse (words[0], out a))
            return -1;
        if (a > 100)
            return -1;
        byte b;
        if (!Byte.TryParse (words[1], out b))
            return -1;
        if (b > 100)
            return -1;
        byte c;
        if (!Byte.TryParse (words[2], out c))
            return -1;
        if (c > 100)
            return -1;
        byte n;
        if (!Byte.TryParse (words[3], out n))
            return -1;
        if (n > 100)
            return -1;
        short ans = Convert.ToInt16 (a >= c && b >= c ? a+b-c : -1);
        ans = Convert.ToInt16 (ans > -1 && n > ans ? n-ans : -1);
        Console.WriteLine (ans);
        return 0;
        }

    static string[] ReadSplitLine (uint numWordNeed)
        {
        string line = Console.ReadLine ();
        if (line == null)
            return null;
        char[] delims = new char[2] {' ', '\t'};
        string[] words = line.Split (delims,StringSplitOptions.RemoveEmptyEntries);
        return (words.Length == numWordNeed ? words : null);
        }
    }

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading;

namespace ReadWriteTemplate
{
    public static class Solver
    {
        private static void SolveCase()
        {
            int a = ReadInt();
            int b = ReadInt();
            int c = ReadInt();
            int n = ReadInt();

            int d = n - a - b + c;

            if (d <= 0 || c > a || c > b)
            {
                Writer.WriteLine(-1);
                return;
            }

            Writer.WriteLine(d);
        }

        public static void Solve()
        {
            SolveCase();

            /*var sw = Stopwatch.StartNew();*/

            /*int T = ReadInt();
            for (int i = 0; i < T; i++)
            {
                Writer.Write("Case #{0}: ", i + 1);
                SolveCase();
            }*/

            /*sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);*/
        }

        public static void Main()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

#if DEBUG
            //Reader = Console.In; Writer = Console.Out;
            Reader = File.OpenText("input.txt"); Writer = File.CreateText("output.txt");
#else
            Reader = Console.In; Writer = Console.Out;
#endif

            // Solve();
            Thread thread = new Thread(Solve, 64 * 1024 * 1024);
            thread.CurrentCulture = CultureInfo.InvariantCulture;
            thread.Start();
            thread.Join();

            Reader.Close();
            Writer.Close();
        }

        public static IOrderedEnumerable<TSource> OrderByWithShuffle<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            return source.Shuffle().OrderBy(keySelector);
        }

        public static T[] Shuffle<T>(this IEnumerable<T> source)
        {
            T[] result = source.ToArray();
            Random rnd = new Random();
            for (int i = result.Length - 1; i >= 1; i--)
            {
                int k = rnd.Next(i + 1);
                T tmp = result[k];
                result[k] = result[i];
                result[i] = tmp;
            }
            return result;
        }

        #region Read/Write

        private static TextReader Reader;

        private static TextWriter Writer;

        private static Queue<string> CurrentLineTokens = new Queue<string>();

        private static string[] ReadAndSplitLine()
        {
            return Reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
        }

        public static string ReadToken()
        {
            while (CurrentLineTokens.Count == 0)
                CurrentLineTokens = new Queue<string>(ReadAndSplitLine());
            return CurrentLineTokens.Dequeue();
        }

        public static string ReadLine()
        {
            return Reader.ReadLine();
        }

        public static int ReadInt()
        {
            return int.Parse(ReadToken());
        }

        public static long ReadLong()
        {
            return long.Parse(ReadToken());
        }

        public static double ReadDouble()
        {
            return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
        }

        public static int[] ReadIntArray()
        {
            return ReadAndSplitLine().Select(int.Parse).ToArray();
        }

        public static long[] ReadLongArray()
        {
            return ReadAndSplitLine().Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
        }

        public static int[][] ReadIntMatrix(int numberOfRows)
        {
            int[][] matrix = new int[numberOfRows][];
            for (int i = 0; i < numberOfRows; i++)
                matrix[i] = ReadIntArray();
            return matrix;
        }

        public static string[] ReadLines(int quantity)
        {
            string[] lines = new string[quantity];
            for (int i = 0; i < quantity; i++)
                lines[i] = Reader.ReadLine().Trim();
            return lines;
        }

        public static void WriteArray<T>(IEnumerable<T> array)
        {
            Writer.WriteLine(string.Join(" ", array));
        }

        #endregion
    }
}

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading;

namespace C_
{
    class Program
    {
         private static TextReader Reader;
         private static TextWriter Writer;
        static void Main(string[] args)
        {

            new A().func();

        }
    }

public class A{
    public int min(int a, int b){
        if(a<b) return a;
        return b;
    }
    public void func(){
        string str= Console.ReadLine();
        string[] arr = str.Split(" ");
        int a = Convert.ToInt32(arr[0]);
        int b = Convert.ToInt32(arr[1]);
        int c = Convert.ToInt32(arr[2]);
        int n = Convert.ToInt32(arr[3]);
        if(c>a || c>b || b>n || a>n || c>n){
             Console.WriteLine("-1");
             return;
        }
        a-=c ; b-=c ;
        int total = a+b+c;
        if((n-total)<1) Console.WriteLine("-1");
        else Console.WriteLine(n-total);
    }
}
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

namespace Task
{
  class Program
  {
    #region Read / Write
    protected static TextReader reader;
    protected static TextWriter writer;
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0) currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++) matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
      int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
      for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++) ret[i][j] = matrix[j][i]; }
      return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++) lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array) writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
      public new TValue this[TKey key]
      {
        get { return ContainsKey(key) ? base[key] : default(TValue); }
        set { base[key] = value; }
      }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++) ret[i] = new T(); return ret; }
    #endregion

    public struct Threesome
    {
      public Threesome(int item1, int item2, int item3)
      {
        Item1 = item1;
        Item2 = item2;
        Item3 = item3;
      }

      public int Item1;
      public int Item2;
      public int Item3;
    }

    public static bool NextPermutation(List<int> perm)
    {
      for (int i = perm.Count - 2; i >= 0; i--)
      {
        if (perm[i] < perm[i + 1])
        {
          int min = i + 1;
          for (int j = min; j < perm.Count; j++)
            if (perm[j] > perm[i] && perm[j] < perm[min])
              min = j;

          int tmp = perm[i];
          perm[i] = perm[min];
          perm[min] = tmp;
          perm.Reverse(i + 1, perm.Count - i - 1);
          return true;
        }
      }

      return false;
    }

    static void Main(string[] args)
    {
      int i;
      reader = new StreamReader(Console.OpenStandardInput());
      writer = new StreamWriter(Console.OpenStandardOutput());

      //int n = ReadInt();
      //var inpMas = ReadIntArray();
      //int fiveCount = inpMas.Count(it => it == 5);
      //int need5 = n / 2 + (n & 1);
      //if (fiveCount >= need5)
      //{
      //  Write(0);
      //}
      //else
      //{
      //  Write(need5 - fiveCount);
      //}

    int A = ReadInt();
    int B = ReadInt();
    int C = ReadInt();
    int N = ReadInt();
      //var inpMas2 = ReadIntArray();

    if (A >= N || B >= N || C >= N || C > A || C > B)
    {
      Write(-1);
    }
    else
    {
      int ans = N - A - B + C;
      if (ans <= 0)
        ans = -1;

      Write(ans);
      //WriteArray(mas.Select(item => item[0]));
    }

      reader.Close();
      writer.Close();
    }
  }
}

using System;

namespace CSharp
{
    public class _991A
    {
        public static void Main()
        {
            string[] tokens = Console.ReadLine().Split();

            int a = int.Parse(tokens[0]);
            int b = int.Parse(tokens[1]);
            int c = int.Parse(tokens[2]);
            int n = int.Parse(tokens[3]);

            if (a + b - c > n | c > a || c > b)
            {
                Console.WriteLine(-1);
            }
            else
            {
                int failed = n - a - b + c;
                Console.WriteLine(failed <= 0 ? -1 : failed);
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main()
        {
            string[] student = Console.ReadLine().Split(' ');
            int a = int.Parse(student[0]);
            int b = int.Parse(student[1]);
            int c = int.Parse(student[2]);
            int n = int.Parse(student[3]);
            if (a > n || b > n || c > a || c > b || ((a - c) + (b - c) + c >= n) || a == n || b == n)
            {
                Console.WriteLine(-1);
            }
            else
            {
                Console.WriteLine(n - ((a - c) + (b - c) + c));
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] y = Console.ReadLine().Split(' ');
            int a = int.Parse(y[0]);
            int b = int.Parse(y[1]);
            int c = int.Parse(y[2]);
            int d = int.Parse(y[3]);
            int q = 0;
            int w = 0;
            int sum = 0;
            if ((a > d) || (b > d) || (c > d))
                Console.WriteLine(-1);
            else
            {
                q = a - c;
                w = b - c;
                if ((q < 0) || (w < 0))
                    Console.WriteLine(-1);
                else
                {
                    sum = q + w + c;
                    if (sum > d)
                        Console.WriteLine(-1);
                    else
                    {
                        if (d - sum > 0)

                            Console.WriteLine(d - sum);
                        else
                            Console.WriteLine(-1);
                    }
                }

            }

            //Console.ReadKey();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace comar
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int a = arr[0];
            int b = arr[1];
            int c = arr[2];
            int n = arr[3];
            if (c > a || c > b)
            {
                Console.WriteLine(-1);
                return;
            }
            int calc = n - ((a-c) + (b-c)+c);
            if (calc >=1)
            {
                Console.WriteLine(calc);
            }
            else
            {
                Console.WriteLine(-1);
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.IO;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string s = Console.ReadLine();
            string []ast = s.Split(new char[]{' '});
            int a = int.Parse(ast[0]);
            int b = int.Parse(ast[1]);
            int c = int.Parse(ast[2]);
            int n = int.Parse(ast[3]);

            int otv = n - ((a - c) + (b - c) + c);
            if((otv > 0) && (n > c) && (a >= c) && (b >= c))
            Console.WriteLine(otv);
            else
            Console.WriteLine("-1");
        }
    }
}

using System;
using System.IO;
using System.Text;

namespace If_at_first_you_dont_succeed
{
    class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024 * 10), Encoding.ASCII, false, 1024 * 10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024 * 10), Encoding.ASCII, 1024 * 10);

        private static void Main(string[] args)
        {
            writer.WriteLine(Solve(args));
            writer.Flush();
        }

        private static int Solve(string[] args)
        {
            int A = Next(), B = Next(), C = Next(), N = Next();

            if (A > N || B > N || C > A || C > B)
                return -1;

            int ans = N - A - B + C;
            if (ans > 0)
                return ans;
            return -1;
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;

public class MainClass
{
    private static object lockFile = new object();

    public static void Main(string[] args)
    {
        int[] abcn = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();

        int a = abcn[0];
        int b = abcn[1];
        int c = abcn[2];
        int n = abcn[3];

        int ans = n - (a + b - c);

        if (ans <= 0 || c > a || c>b) Console.WriteLine(-1);
        else Console.WriteLine(ans);

      //  Console.ReadKey();
    }
    static decimal average (int [] ar)
    {
        decimal sum = 0;

        for (int i = 0; i < ar.Length; i++)
        {
            sum += ar[i];
        }

        decimal avg = sum / ar.Length;

        decimal fraction = avg - Math.Truncate(avg);
        if (fraction < 0.5m) return Math.Truncate(avg);

        return Math.Truncate(avg) + 1;
    }
    static void changeTheMark (int [] ar)
    {
        for (int i = 0; i < ar.Length; i++)
        {
            if (ar[i] != 5)
            {
                ar[i] = 5;
                break;
            }
        }
    }

    static long sum (long [] [] ar, int n)
    {
        long sum = 0;

        for (int i = 0; i < ar[n - 1].Length; i++)
        {
            sum += ar[n-1][i];
        }

        return sum;
    }

    static long [] []  solve (int n)
    {
        long[][] ar = new long[n][];

        for (int i = 0; i < n; i++)
        {
            ar[i] = new long[10];
        }

        for (int i = 0; i < 10; i++)
        {
            ar[0][i] = 1;
        }

        for (int i = 1; i < n; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                if(j == 0)
                {
                    ar[i][j] = ar[i - 1][j] + ar[i - 1][j + 1];
                }
                else if(j==9)
                {
                    ar[i][j] = ar[i - 1][j] + ar[i - 1][j - 1];
                }
                else
                {
                    ar[i][j] = ar[i - 1][j - 1] + ar[i - 1][j] + ar[i - 1][j + 1];
                }
            }
        }

        return ar;
    }
    private static long lowerBound(long w, long h, long n)
    {
        long lo = 1;
        long hi = Math.Max(w * n, h * n);
        long med = lo + (hi - lo) / 2;

        while (lo <= hi)
        {
            med = lo + (hi - lo) / 2;
            long ans = (med / w) * (med / h);
            Console.WriteLine(med + " " +ans);
            if (ans > n) hi = med - 1;
            else if (ans < n) lo = med + 1;
            else return med;
        }

        long ans2 = (med / w) * (med / h);
        return ans2 >=n ? med : med +1;
    }

    private static long sumOfNumbers(long n)
    {
        long sum = 0;

        while (n > 0)
        {
            sum += n % 10;
            n /= 10;
        }

        return sum;
    }
    private static long gcd (long a, long b)
    {
       return b== 0 ? a : gcd(b, a % b);
    }
    private static long[] getPowersOfTwoAnotherMethod()
    {
        long[] array = new long[63];

        for (int i = 0; i < array.Length; i++)
        {
            array[i] = (long)Math.Pow(2, i);
        }

        return array;
    }

    private static void testOut(out int n)
    {
        n = 10;
        throw new InvalidOperationException("Inner exception");
    }

    private static string toBinary(BigInteger n)
    {
        string res = "";

        while (n > 0)
        {
            res = n % 2 + res;
            n /= 2;
        }

        return res;
    }

    private static bool checkTheNumber(int n)
    {
        string binary = toBinary(n);

        return binary.Any(c => c == '0');
    }

    private static int findPosition(string[] array, int currentPosition)
    {
        for (int i = currentPosition; i < array.Length; i++)
        {
            if (array[i] == null) return i;
        }

        return -1;
    }

    private static int findPosition(int currentPosition, string[] array) => 0;

    private static void Foo(StringBuilder fooSb)
    {
        fooSb.Append("In method");
        fooSb = null;
    }

    protected static void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
    {
        int i = 0;
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine(i++);
    }

    static void wrintInFile(char letter, int times, int sleepTime)
    {
        for (int i = 0; i < times; i++)
        {
            lock (lockFile)
            {
                using (StreamWriter writer = new StreamWriter("output.txt", true))
                {
                    writer.WriteLine($"{letter}\t{Thread.CurrentThread.ManagedThreadId}\t{DateTime.Now}");
                }

                Thread.Sleep(sleepTime);
            }
        }
    }

    private static async Task<int> Test(int n)
    {
        Console.WriteLine("Inside await");

        int res = 0;

        await Task.Run(() =>
        {
            for (int i = 0; i < n; i++)
            {
                res += i * i;
                Thread.Sleep(i);
            }
        });

        return res;
    }

    public static void getDirectories(string path)
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(path);

        Console.WriteLine(directoryInfo.FullName);
        PrintAllFileNamesInDirectory(0, directoryInfo.GetFiles());

        DirectoryInfo[] directories = null;

        try
        {
            directories = directoryInfo.GetDirectories();

            foreach (var d in directories)
            {
                getDirectories(d.FullName);
            }
        }
        catch (Exception e)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("COULD NOT HAVE ACCESS TO FOLDER");
        }

    }

    public static void PrintAllFileNamesInDirectory(int numberOfSpaces, FileInfo[] files)
    {
        foreach (FileInfo dir in files)
        {
            Console.WriteLine($"{new string('\t', numberOfSpaces)} {dir.FullName}");
        }

    }

}

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading;

namespace C_
{
    class Program
    {
         private static TextReader Reader;
         private static TextWriter Writer;
        static void Main(string[] args)
        {

            new A().func();

        }
    }

public class A{
    public int min(int a, int b){
        if(a<b) return a;
        return b;
    }
    public void func(){
        string str= Console.ReadLine();
        string[] arr = str.Split(" ");
        int a = Convert.ToInt32(arr[0]);
        int b = Convert.ToInt32(arr[1]);
        int c = Convert.ToInt32(arr[2]);
        int n = Convert.ToInt32(arr[3]);
        if(c>a || c>b || b>n || a>n || c>n){
             Console.WriteLine("-1");
             return;
        }
        a-=c ; b-=c ;
        int total = a+b;
        if((n-total)<1) Console.WriteLine("-1");
        else Console.WriteLine(n-total);
    }
}
}

using System;

namespace CSharp
{
    public class _991A
    {
        public static void Main()
        {
            string[] tokens = Console.ReadLine().Split();

            int a = int.Parse(tokens[0]);
            int b = int.Parse(tokens[1]);
            int c = int.Parse(tokens[2]);
            int n = int.Parse(tokens[3]);

            if (a + b > n | c > a || c > b)
            {
                Console.WriteLine(-1);
            }
            else
            {
                int failed = n - a - b + c;
                Console.WriteLine(failed <= 0 ? -1 : failed);
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main()
        {
            string[] student = Console.ReadLine().Split(' ');
            int a = int.Parse(student[0]);
            int b = int.Parse(student[1]);
            int c = int.Parse(student[2]);
            int n = int.Parse(student[3]);
            if (a > n || b > n || c > a || c > b || ((a - c) + (b - c) + c == n))
            {
                Console.WriteLine(-1);
            }
            else
            {
                Console.WriteLine(n - ((a - c) + (b - c) + c));
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main()
        {
            string[] student = Console.ReadLine().Split(' ');
            int a = int.Parse(student[0]);
            int b = int.Parse(student[1]);
            int c = int.Parse(student[2]);
            int n = int.Parse(student[3]);
            if (a > n || b > n || c > a || c > b || ((a - c) + (b - c) + c == n) || a == n || b == n)
            {
                Console.WriteLine(-1);
            }
            else
            {
                Console.WriteLine(n - ((a - c) + (b - c) + c));
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main()
        {
            string[] student = Console.ReadLine().Split(' ');
            int a = int.Parse(student[0]);
            int b = int.Parse(student[1]);
            int c = int.Parse(student[2]);
            int n = int.Parse(student[3]);
            if (a > n || b > n || ((a - c) + (b - c) + c == n))
            {
                Console.WriteLine(-1); Console.ReadLine();
            }
            else
            {
                Console.WriteLine(n - ((a - c) + (b - c) + c));
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace comar
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int a = arr[0];
            int b = arr[1];
            int c = arr[2];
            int n = arr[3];
            if (c > a && c > b)
            {
                Console.WriteLine(-1);
                return;
            }
            int calc = n - ((a+b) -c);
            if (calc >=1)
            {
                Console.WriteLine(calc);
            }
            else
            {
                Console.WriteLine(-1);
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace comar
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int a = arr[0];
            int b = arr[1];
            int c = arr[2];
            int n = arr[3];
            int calc = n - ((a+b) -c);
            if (calc >=1)
            {
                Console.WriteLine(calc);
            }
            else
            {
                Console.WriteLine(-1);
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace comar
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int a = arr[0];
            int b = arr[1];
            int c = arr[2];
            int n = arr[3];
            if (c > a && c > b)
            {
                Console.WriteLine(-1);
                return;
            }
            int calc = n - ((a-c) + (b-c)+c);
            if (calc >=1)
            {
                Console.WriteLine(calc);
            }
            else
            {
                Console.WriteLine(-1);
            }
        }
    }
}

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            int a = input[0];
            int b = input[1];
            int c = input[2];
            int n = input[3];
            if ((a < c) || (b < c) || (a + b - c < 0) || (c > n - 1) || (a + b - c > n -1)) {
                Console.WriteLine("-1");
            }
            else {
                int result = n - (a + b - c);
                if (result > 1)
                {
                    Console.WriteLine(result);
                }
                else {
                    Console.WriteLine("-1");
                }
            }
        }
    }
}

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            int a = input[0];
            int b = input[1];
            int c = input[2];
            int n = input[3];
            if ((a + b - c < 0) || (c > n - 1)) {
                Console.WriteLine("-1");
            }
            else {
                int result = n - (a + b - c);
                if (result > 1)
                {
                    Console.WriteLine(result);
                }
                else {
                    Console.WriteLine("-1");
                }
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.IO;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string s = Console.ReadLine();
            string []ast = s.Split(new char[]{' '});
            int a = int.Parse(ast[0]);
            int b = int.Parse(ast[1]);
            int c = int.Parse(ast[2]);
            int n = int.Parse(ast[3]);

            int otv = n - ((a - c) + (b - c) + c);
            if((otv > 0) && (n > c) && (a > c) && (b > c))
            Console.WriteLine(otv);
            else
            Console.WriteLine("-1");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.IO;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string s = Console.ReadLine();
            string []ast = s.Split(new char[]{' '});
            int a = int.Parse(ast[0]);
            int b = int.Parse(ast[1]);
            int c = int.Parse(ast[2]);
            int n = int.Parse(ast[3]);

            int otv = n - ((a - c) + (b - c) + c);
            if((otv > 0) && (n > c))
            Console.WriteLine(otv);
            else
            Console.WriteLine("-1");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.IO;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string s = Console.ReadLine();
            string []ast = s.Split(new char[]{' '});
            int a = int.Parse(ast[0]);
            int b = int.Parse(ast[1]);
            int c = int.Parse(ast[2]);
            int n = int.Parse(ast[3]);

            int otv = n - ((a - c) + (b - c) + c);
            if(otv > 0)
            Console.WriteLine(otv);
            else
            Console.WriteLine("-1");
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;

public class MainClass
{
    private static object lockFile = new object();

    public static void Main(string[] args)
    {
        int[] abcn = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();

        int a = abcn[0];
        int b = abcn[1];
        int c = abcn[2];
        int n = abcn[3];

        int ans = n - (a + b - c);

        if (ans <= 0) Console.WriteLine(-1);
        else Console.WriteLine(ans);

      //  Console.ReadKey();
    }
    static decimal average (int [] ar)
    {
        decimal sum = 0;

        for (int i = 0; i < ar.Length; i++)
        {
            sum += ar[i];
        }

        decimal avg = sum / ar.Length;

        decimal fraction = avg - Math.Truncate(avg);
        if (fraction < 0.5m) return Math.Truncate(avg);

        return Math.Truncate(avg) + 1;
    }
    static void changeTheMark (int [] ar)
    {
        for (int i = 0; i < ar.Length; i++)
        {
            if (ar[i] != 5)
            {
                ar[i] = 5;
                break;
            }
        }
    }

    static long sum (long [] [] ar, int n)
    {
        long sum = 0;

        for (int i = 0; i < ar[n - 1].Length; i++)
        {
            sum += ar[n-1][i];
        }

        return sum;
    }

    static long [] []  solve (int n)
    {
        long[][] ar = new long[n][];

        for (int i = 0; i < n; i++)
        {
            ar[i] = new long[10];
        }

        for (int i = 0; i < 10; i++)
        {
            ar[0][i] = 1;
        }

        for (int i = 1; i < n; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                if(j == 0)
                {
                    ar[i][j] = ar[i - 1][j] + ar[i - 1][j + 1];
                }
                else if(j==9)
                {
                    ar[i][j] = ar[i - 1][j] + ar[i - 1][j - 1];
                }
                else
                {
                    ar[i][j] = ar[i - 1][j - 1] + ar[i - 1][j] + ar[i - 1][j + 1];
                }
            }
        }

        return ar;
    }
    private static long lowerBound(long w, long h, long n)
    {
        long lo = 1;
        long hi = Math.Max(w * n, h * n);
        long med = lo + (hi - lo) / 2;

        while (lo <= hi)
        {
            med = lo + (hi - lo) / 2;
            long ans = (med / w) * (med / h);
            Console.WriteLine(med + " " +ans);
            if (ans > n) hi = med - 1;
            else if (ans < n) lo = med + 1;
            else return med;
        }

        long ans2 = (med / w) * (med / h);
        return ans2 >=n ? med : med +1;
    }

    private static long sumOfNumbers(long n)
    {
        long sum = 0;

        while (n > 0)
        {
            sum += n % 10;
            n /= 10;
        }

        return sum;
    }
    private static long gcd (long a, long b)
    {
       return b== 0 ? a : gcd(b, a % b);
    }
    private static long[] getPowersOfTwoAnotherMethod()
    {
        long[] array = new long[63];

        for (int i = 0; i < array.Length; i++)
        {
            array[i] = (long)Math.Pow(2, i);
        }

        return array;
    }

    private static void testOut(out int n)
    {
        n = 10;
        throw new InvalidOperationException("Inner exception");
    }

    private static string toBinary(BigInteger n)
    {
        string res = "";

        while (n > 0)
        {
            res = n % 2 + res;
            n /= 2;
        }

        return res;
    }

    private static bool checkTheNumber(int n)
    {
        string binary = toBinary(n);

        return binary.Any(c => c == '0');
    }

    private static int findPosition(string[] array, int currentPosition)
    {
        for (int i = currentPosition; i < array.Length; i++)
        {
            if (array[i] == null) return i;
        }

        return -1;
    }

    private static int findPosition(int currentPosition, string[] array) => 0;

    private static void Foo(StringBuilder fooSb)
    {
        fooSb.Append("In method");
        fooSb = null;
    }

    protected static void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
    {
        int i = 0;
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine(i++);
    }

    static void wrintInFile(char letter, int times, int sleepTime)
    {
        for (int i = 0; i < times; i++)
        {
            lock (lockFile)
            {
                using (StreamWriter writer = new StreamWriter("output.txt", true))
                {
                    writer.WriteLine($"{letter}\t{Thread.CurrentThread.ManagedThreadId}\t{DateTime.Now}");
                }

                Thread.Sleep(sleepTime);
            }
        }
    }

    private static async Task<int> Test(int n)
    {
        Console.WriteLine("Inside await");

        int res = 0;

        await Task.Run(() =>
        {
            for (int i = 0; i < n; i++)
            {
                res += i * i;
                Thread.Sleep(i);
            }
        });

        return res;
    }

    public static void getDirectories(string path)
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(path);

        Console.WriteLine(directoryInfo.FullName);
        PrintAllFileNamesInDirectory(0, directoryInfo.GetFiles());

        DirectoryInfo[] directories = null;

        try
        {
            directories = directoryInfo.GetDirectories();

            foreach (var d in directories)
            {
                getDirectories(d.FullName);
            }
        }
        catch (Exception e)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("COULD NOT HAVE ACCESS TO FOLDER");
        }

    }

    public static void PrintAllFileNamesInDirectory(int numberOfSpaces, FileInfo[] files)
    {
        foreach (FileInfo dir in files)
        {
            Console.WriteLine($"{new string('\t', numberOfSpaces)} {dir.FullName}");
        }

    }

}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){

		int n = A + B - C;
		if(N > n){

			Console.WriteLine(N - n);
		} else {
			Console.WriteLine(-1);
		}

	}
	int A, B, C, N;
	public Sol(){
		var d = ria();
		A = d[0]; B = d[1]; C = d[2]; N = d[3];
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}

using System;

namespace CF_991_A
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int A = int.Parse(s[0]);
            int B = int.Parse(s[1]);
            int C = int.Parse(s[2]);
            int N = int.Parse(s[3]);

            if(A>N || B>N || C>N)
            {
                Console.WriteLine(-1);
                return;
            }

            int k = N - (A + B - C);
            if (k <= 0)
                Console.WriteLine(-1);
            else
                Console.WriteLine(k);
        }
    }
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

namespace Task
{
  class Program
  {
    #region Read / Write
    protected static TextReader reader;
    protected static TextWriter writer;
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0) currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++) matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
      int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
      for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++) ret[i][j] = matrix[j][i]; }
      return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++) lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array) writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
      public new TValue this[TKey key]
      {
        get { return ContainsKey(key) ? base[key] : default(TValue); }
        set { base[key] = value; }
      }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++) ret[i] = new T(); return ret; }
    #endregion

    public struct Threesome
    {
      public Threesome(int item1, int item2, int item3)
      {
        Item1 = item1;
        Item2 = item2;
        Item3 = item3;
      }

      public int Item1;
      public int Item2;
      public int Item3;
    }

    public static bool NextPermutation(List<int> perm)
    {
      for (int i = perm.Count - 2; i >= 0; i--)
      {
        if (perm[i] < perm[i + 1])
        {
          int min = i + 1;
          for (int j = min; j < perm.Count; j++)
            if (perm[j] > perm[i] && perm[j] < perm[min])
              min = j;

          int tmp = perm[i];
          perm[i] = perm[min];
          perm[min] = tmp;
          perm.Reverse(i + 1, perm.Count - i - 1);
          return true;
        }
      }

      return false;
    }

    static void Main(string[] args)
    {
      int i;
      reader = new StreamReader(Console.OpenStandardInput());
      writer = new StreamWriter(Console.OpenStandardOutput());

      int A = ReadInt();
      int B = ReadInt();
      int C = ReadInt();
      int N = ReadInt();
      //var inpMas2 = ReadIntArray();

      if (A >= N || B >= N || C >= N)
      {
        Write(-1);
        return;
      }

      int ans = N - A - B + C;
      if (ans <= 0)
        ans = -1;

      Write(ans);
      //WriteArray(mas.Select(item => item[0]));

      reader.Close();
      writer.Close();
    }
  }
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

namespace Task
{
  class Program
  {
    #region Read / Write
    protected static TextReader reader;
    protected static TextWriter writer;
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0) currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++) matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
      int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
      for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++) ret[i][j] = matrix[j][i]; }
      return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++) lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array) writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
      public new TValue this[TKey key]
      {
        get { return ContainsKey(key) ? base[key] : default(TValue); }
        set { base[key] = value; }
      }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++) ret[i] = new T(); return ret; }
    #endregion

    public struct Threesome
    {
      public Threesome(int item1, int item2, int item3)
      {
        Item1 = item1;
        Item2 = item2;
        Item3 = item3;
      }

      public int Item1;
      public int Item2;
      public int Item3;
    }

    public static bool NextPermutation(List<int> perm)
    {
      for (int i = perm.Count - 2; i >= 0; i--)
      {
        if (perm[i] < perm[i + 1])
        {
          int min = i + 1;
          for (int j = min; j < perm.Count; j++)
            if (perm[j] > perm[i] && perm[j] < perm[min])
              min = j;

          int tmp = perm[i];
          perm[i] = perm[min];
          perm[min] = tmp;
          perm.Reverse(i + 1, perm.Count - i - 1);
          return true;
        }
      }

      return false;
    }

    static void Main(string[] args)
    {
      int i;
      reader = new StreamReader(Console.OpenStandardInput());
      writer = new StreamWriter(Console.OpenStandardOutput());

      int A = ReadInt();
      int B = ReadInt();
      int C = ReadInt();
      int N = ReadInt();
      //var inpMas2 = ReadIntArray();

      int ans = N - A - B + C;
      if (ans <= 0)
        ans = -1;

      Write(ans);
      //WriteArray(mas.Select(item => item[0]));

      reader.Close();
      writer.Close();
    }
  }
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

namespace Task
{
  class Program
  {
    #region Read / Write
    protected static TextReader reader;
    protected static TextWriter writer;
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0) currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++) matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
      int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
      for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++) ret[i][j] = matrix[j][i]; }
      return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++) lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array) writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
      public new TValue this[TKey key]
      {
        get { return ContainsKey(key) ? base[key] : default(TValue); }
        set { base[key] = value; }
      }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++) ret[i] = new T(); return ret; }
    #endregion

    public struct Threesome
    {
      public Threesome(int item1, int item2, int item3)
      {
        Item1 = item1;
        Item2 = item2;
        Item3 = item3;
      }

      public int Item1;
      public int Item2;
      public int Item3;
    }

    public static bool NextPermutation(List<int> perm)
    {
      for (int i = perm.Count - 2; i >= 0; i--)
      {
        if (perm[i] < perm[i + 1])
        {
          int min = i + 1;
          for (int j = min; j < perm.Count; j++)
            if (perm[j] > perm[i] && perm[j] < perm[min])
              min = j;

          int tmp = perm[i];
          perm[i] = perm[min];
          perm[min] = tmp;
          perm.Reverse(i + 1, perm.Count - i - 1);
          return true;
        }
      }

      return false;
    }

    static void Main(string[] args)
    {
      int i;
      reader = new StreamReader(Console.OpenStandardInput());
      writer = new StreamWriter(Console.OpenStandardOutput());

      //int n = ReadInt();
      //var inpMas = ReadIntArray();
      //int fiveCount = inpMas.Count(it => it == 5);
      //if (fiveCount * 2 >= n)
      //{
      //  Write(0);
      //}
      //else
      //{
      //  Write();
      //}

      int A = ReadInt();
      int B = ReadInt();
      int C = ReadInt();
      int N = ReadInt();
      //var inpMas2 = ReadIntArray();

      if (A >= N || B >= N || C >= N)
      {
        Write(-1);
      }
      else
      {

        int ans = N - A - B + C;
        if (ans <= 0)
          ans = -1;

        Write(ans);
        //WriteArray(mas.Select(item => item[0]));
      }

      reader.Close();
      writer.Close();
    }
  }
}