import java.util.*;
class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        long a = scanner.nextLong();
        long b = scanner.nextLong();
        long c = scanner.nextLong();
        long k = scanner.nextLong();
        long ans = k;
        if(k <= a){System.out.println(ans);return;}
        else if(k <= a+b){System.out.println(ans-(k-a));return;}
        else{System.out.println(ans-b-(k-a-b)*2);return;}
    }
}
 import java.util.*; util.* java class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        long a = scanner.nextLong();
        long b = scanner.nextLong();
        long c = scanner.nextLong();
        long k = scanner.nextLong();
        long ans = k;
        if(k <= a){System.out.println(ans);return;}
        else if(k <= a+b){System.out.println(ans-(k-a));return;}
        else{System.out.println(ans-b-(k-a-b)*2);return;}
    }
} Main public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        long a = scanner.nextLong();
        long b = scanner.nextLong();
        long c = scanner.nextLong();
        long k = scanner.nextLong();
        long ans = k;
        if(k <= a){System.out.println(ans);return;}
        else if(k <= a+b){System.out.println(ans-(k-a));return;}
        else{System.out.println(ans-b-(k-a-b)*2);return;}
    } main {
        Scanner scanner = new Scanner(System.in);
        long a = scanner.nextLong();
        long b = scanner.nextLong();
        long c = scanner.nextLong();
        long k = scanner.nextLong();
        long ans = k;
        if(k <= a){System.out.println(ans);return;}
        else if(k <= a+b){System.out.println(ans-(k-a));return;}
        else{System.out.println(ans-b-(k-a-b)*2);return;}
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) long a = scanner.nextLong(); a scanner.nextLong() scanner.nextLong scanner long b = scanner.nextLong(); b scanner.nextLong() scanner.nextLong scanner long c = scanner.nextLong(); c scanner.nextLong() scanner.nextLong scanner long k = scanner.nextLong(); k scanner.nextLong() scanner.nextLong scanner long ans = k; ans k if(k <= a){System.out.println(ans);return;}
        else if(k <= a+b){System.out.println(ans-(k-a));return;}
        else{System.out.println(ans-b-(k-a-b)*2);return;} k <= a k a {System.out.println(ans);return;} System.out.println(ans) System.out.println System.out System System.out ans return; if(k <= a+b){System.out.println(ans-(k-a));return;}
        else{System.out.println(ans-b-(k-a-b)*2);return;} k <= a+b k a+b a b {System.out.println(ans-(k-a));return;} System.out.println(ans-(k-a)) System.out.println System.out System System.out ans-(k-a) ans (k-a) k a return; {System.out.println(ans-b-(k-a-b)*2);return;} System.out.println(ans-b-(k-a-b)*2) System.out.println System.out System System.out ans-b-(k-a-b)*2 ans-b-(k-a-b)*2 ans b (k-a-b)*2 (k-a-b) k-a-b k a b 2 return; String[] args args
import java.util.*;
class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        long a = scanner.nextLong();
        long b = scanner.nextLong();
        long c = scanner.nextLong();
        long k = scanner.nextLong();
        long ans = k;
        if(k <= a){System.out.println(ans);return;}
        else if(k <= a+b){System.out.println(ans-(k-a));return;}
        else{System.out.println(ans-b-(k-a-b)*2);return;}
    }
}

