import java.util.*; 
import java.io.*;

public class Main {
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
 
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));

        
        int A = nextInt();//health 
        int B = nextInt();//strength 
        int C = nextInt();
        int K = nextInt();
 
        int sum = 0;
  
        if(K>=A){
            sum += A;
            K = K-A;
        }
        else{
            sum += K;
            K=0;
        }
        if(K>=B){  
            K = K-B;
        } 
        else{
            K=0;
        }
        if(K>=C){
            sum += -C;
            K = K-A;
        }
        else{
            sum += -K;
        }
  
        System.out.println(sum);
    }
    
      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
} import java.util.*; util.* java import java.io.*; io.* java public class Main {
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
 
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));

        
        int A = nextInt();//health 
        int B = nextInt();//strength 
        int C = nextInt();
        int K = nextInt();
 
        int sum = 0;
  
        if(K>=A){
            sum += A;
            K = K-A;
        }
        else{
            sum += K;
            K=0;
        }
        if(K>=B){  
            K = K-B;
        } 
        else{
            K=0;
        }
        if(K>=C){
            sum += -C;
            K = K-A;
        }
        else{
            sum += -K;
        }
  
        System.out.println(sum);
    }
    
      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
} Main static StringTokenizer st; st static BufferedReader br; br static PrintWriter out; out public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));

        
        int A = nextInt();//health 
        int B = nextInt();//strength 
        int C = nextInt();
        int K = nextInt();
 
        int sum = 0;
  
        if(K>=A){
            sum += A;
            K = K-A;
        }
        else{
            sum += K;
            K=0;
        }
        if(K>=B){  
            K = K-B;
        } 
        else{
            K=0;
        }
        if(K>=C){
            sum += -C;
            K = K-A;
        }
        else{
            sum += -K;
        }
  
        System.out.println(sum);
    } main {
        br = new BufferedReader(new InputStreamReader(System.in));

        
        int A = nextInt();//health 
        int B = nextInt();//strength 
        int C = nextInt();
        int K = nextInt();
 
        int sum = 0;
  
        if(K>=A){
            sum += A;
            K = K-A;
        }
        else{
            sum += K;
            K=0;
        }
        if(K>=B){  
            K = K-B;
        } 
        else{
            K=0;
        }
        if(K>=C){
            sum += -C;
            K = K-A;
        }
        else{
            sum += -K;
        }
  
        System.out.println(sum);
    } br = new BufferedReader(new InputStreamReader(System.in)) br new BufferedReader(new InputStreamReader(System.in)) int A = nextInt(); A nextInt() nextInt int B = nextInt(); B nextInt() nextInt int C = nextInt(); C nextInt() nextInt int K = nextInt(); K nextInt() nextInt int sum = 0; sum 0 if(K>=A){
            sum += A;
            K = K-A;
        }
        else{
            sum += K;
            K=0;
        } K>=A K A {
            sum += A;
            K = K-A;
        } sum += A sum A K = K-A K K-A K A {
            sum += K;
            K=0;
        } sum += K sum K K=0 K 0 if(K>=B){  
            K = K-B;
        } 
        else{
            K=0;
        } K>=B K B {  
            K = K-B;
        } K = K-B K K-B K B {
            K=0;
        } K=0 K 0 if(K>=C){
            sum += -C;
            K = K-A;
        }
        else{
            sum += -K;
        } K>=C K C {
            sum += -C;
            K = K-A;
        } sum += -C sum -C C K = K-A K K-A K A {
            sum += -K;
        } sum += -K sum -K K System.out.println(sum) System.out.println System.out System System.out sum String[] args args public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    } nextInt {
        return Integer.parseInt(next());
    } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public static long nextLong() throws IOException {
        return Long.parseLong(next());
    } nextLong {
        return Long.parseLong(next());
    } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    } nextDouble {
        return Double.parseDouble(next());
    } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    } next {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    } while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim()); st == null || !st.hasMoreTokens() st == null st null !st.hasMoreTokens() st.hasMoreTokens() st.hasMoreTokens st st = new StringTokenizer(br.readLine().trim()) st new StringTokenizer(br.readLine().trim()) return st.nextToken(); st.nextToken() st.nextToken st public class Main {
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
 
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));

        
        int A = nextInt();//health 
        int B = nextInt();//strength 
        int C = nextInt();
        int K = nextInt();
 
        int sum = 0;
  
        if(K>=A){
            sum += A;
            K = K-A;
        }
        else{
            sum += K;
            K=0;
        }
        if(K>=B){  
            K = K-B;
        } 
        else{
            K=0;
        }
        if(K>=C){
            sum += -C;
            K = K-A;
        }
        else{
            sum += -K;
        }
  
        System.out.println(sum);
    }
    
      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
} public class Main {
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
 
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));

        
        int A = nextInt();//health 
        int B = nextInt();//strength 
        int C = nextInt();
        int K = nextInt();
 
        int sum = 0;
  
        if(K>=A){
            sum += A;
            K = K-A;
        }
        else{
            sum += K;
            K=0;
        }
        if(K>=B){  
            K = K-B;
        } 
        else{
            K=0;
        }
        if(K>=C){
            sum += -C;
            K = K-A;
        }
        else{
            sum += -K;
        }
  
        System.out.println(sum);
    }
    
      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
} Main
import java.util.*; 
import java.io.*;

public class Main {
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
 
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));

        
        int A = nextInt();//health 
        int B = nextInt();//strength 
        int C = nextInt();
        int K = nextInt();
 
        int sum = 0;
  
        if(K>=A){
            sum += A;
            K = K-A;
        }
        else{
            sum += K;
            K=0;
        }
        if(K>=B){  
            K = K-B;
        } 
        else{
            K=0;
        }
        if(K>=C){
            sum += -C;
            K = K-A;
        }
        else{
            sum += -K;
        }
  
        System.out.println(sum);
    }
    
      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
}