import java.util.*;

public class Main{
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        int a,b,c,k;
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        k = sc.nextInt();
        
        int sn = 0 ;
        
        if(a<=k){
            sn += a*1;
            k -= a;
        }
        else{
            sn += k;
            k -= k;
        }
        
        if(b<=k){
            sn += 0;
            k -= b;
        }
        else{
            k -= k;
        }
        
        if(c<=k){
            sn -= 1*c;
            k -= c;
        }
        else{
            sn -= k;
            k -= k;
        }
        
        System.out.println(sn);
        
    }
}
 import java.util.*; util.* java public class Main{
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        int a,b,c,k;
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        k = sc.nextInt();
        
        int sn = 0 ;
        
        if(a<=k){
            sn += a*1;
            k -= a;
        }
        else{
            sn += k;
            k -= k;
        }
        
        if(b<=k){
            sn += 0;
            k -= b;
        }
        else{
            k -= k;
        }
        
        if(c<=k){
            sn -= 1*c;
            k -= c;
        }
        else{
            sn -= k;
            k -= k;
        }
        
        System.out.println(sn);
        
    }
} Main public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        int a,b,c,k;
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        k = sc.nextInt();
        
        int sn = 0 ;
        
        if(a<=k){
            sn += a*1;
            k -= a;
        }
        else{
            sn += k;
            k -= k;
        }
        
        if(b<=k){
            sn += 0;
            k -= b;
        }
        else{
            k -= k;
        }
        
        if(c<=k){
            sn -= 1*c;
            k -= c;
        }
        else{
            sn -= k;
            k -= k;
        }
        
        System.out.println(sn);
        
    } main {
        
        Scanner sc = new Scanner(System.in);
        int a,b,c,k;
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        k = sc.nextInt();
        
        int sn = 0 ;
        
        if(a<=k){
            sn += a*1;
            k -= a;
        }
        else{
            sn += k;
            k -= k;
        }
        
        if(b<=k){
            sn += 0;
            k -= b;
        }
        else{
            k -= k;
        }
        
        if(c<=k){
            sn -= 1*c;
            k -= c;
        }
        else{
            sn -= k;
            k -= k;
        }
        
        System.out.println(sn);
        
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a a b b c c k; k a = sc.nextInt() a sc.nextInt() sc.nextInt sc b = sc.nextInt() b sc.nextInt() sc.nextInt sc c = sc.nextInt() c sc.nextInt() sc.nextInt sc k = sc.nextInt() k sc.nextInt() sc.nextInt sc int sn = 0 ; sn 0 if(a<=k){
            sn += a*1;
            k -= a;
        }
        else{
            sn += k;
            k -= k;
        } a<=k a k {
            sn += a*1;
            k -= a;
        } sn += a*1 sn a*1 a 1 k -= a k a {
            sn += k;
            k -= k;
        } sn += k sn k k -= k k k if(b<=k){
            sn += 0;
            k -= b;
        }
        else{
            k -= k;
        } b<=k b k {
            sn += 0;
            k -= b;
        } sn += 0 sn 0 k -= b k b {
            k -= k;
        } k -= k k k if(c<=k){
            sn -= 1*c;
            k -= c;
        }
        else{
            sn -= k;
            k -= k;
        } c<=k c k {
            sn -= 1*c;
            k -= c;
        } sn -= 1*c sn 1*c 1 c k -= c k c {
            sn -= k;
            k -= k;
        } sn -= k sn k k -= k k k System.out.println(sn) System.out.println System.out System System.out sn String[] args args public class Main{
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        int a,b,c,k;
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        k = sc.nextInt();
        
        int sn = 0 ;
        
        if(a<=k){
            sn += a*1;
            k -= a;
        }
        else{
            sn += k;
            k -= k;
        }
        
        if(b<=k){
            sn += 0;
            k -= b;
        }
        else{
            k -= k;
        }
        
        if(c<=k){
            sn -= 1*c;
            k -= c;
        }
        else{
            sn -= k;
            k -= k;
        }
        
        System.out.println(sn);
        
    }
} public class Main{
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        int a,b,c,k;
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        k = sc.nextInt();
        
        int sn = 0 ;
        
        if(a<=k){
            sn += a*1;
            k -= a;
        }
        else{
            sn += k;
            k -= k;
        }
        
        if(b<=k){
            sn += 0;
            k -= b;
        }
        else{
            k -= k;
        }
        
        if(c<=k){
            sn -= 1*c;
            k -= c;
        }
        else{
            sn -= k;
            k -= k;
        }
        
        System.out.println(sn);
        
    }
} Main
import java.util.*;

public class Main{
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        int a,b,c,k;
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        k = sc.nextInt();
        
        int sn = 0 ;
        
        if(a<=k){
            sn += a*1;
            k -= a;
        }
        else{
            sn += k;
            k -= k;
        }
        
        if(b<=k){
            sn += 0;
            k -= b;
        }
        else{
            k -= k;
        }
        
        if(c<=k){
            sn -= 1*c;
            k -= c;
        }
        else{
            sn -= k;
            k -= k;
        }
        
        System.out.println(sn);
        
    }
}

