import javax.swing.text.html.HTMLDocument;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.nio.Buffer;
import java.util.*;

class Main {
    public static void main(String[] args){
        try {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String[] ss=br.readLine().split(" ");
            long a=Long.parseLong(ss[0]);
            long b=Long.parseLong(ss[1]);
            long c=Long.parseLong(ss[2]);
            long k=Long.parseLong(ss[3]);
            if(k<=a+b){
                System.out.println(Math.min(k,a));
            }else{
                k-=(a+b);
                System.out.println(a-k);
            }
        } catch (Exception e) {
            System.out.println("kkkk "+ e.getMessage());
        }
    }
    static void dfs(ArrayList<HashSet<Integer>> list,boolean[] vis,int i){
        vis[i]=true;
        Iterator<Integer> it=list.get(i).iterator();
        while(it.hasNext()){
            int u=it.next();
            if(!vis[u]){
                dfs(list,vis,u);
            }
        }
    }
    static int modInverse(int x,int m){
        return power(x,m-2,m);
    }
    static int power(int x, int y, int m)
    {
        if (y == 0)
            return 1;

        long p = power(x, y / 2, m) % m;
        p = (p * p) % m;

        return (int)((y%2==0)? p : (x*p)%m);
    }
    static void shuffle(String[] a) {
        Random get = new Random();
        for (int i = 0; i < a.length; i++) {
            int r = get.nextInt(a.length);
            String temp = a[i];
            a[i] = a[r];
            a[r] = temp;
        }
    }
    static class quad{
        int a,b,c,d;
        public quad(int a,int b,int c,int d){
            this.a=a;
            this.b=b;
            this.c=c;
            this.d=d;
        }
    }
} import javax.swing.text.html.HTMLDocument; HTMLDocument javax.swing.text.html import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.PrintWriter; PrintWriter java.io import java.math.BigInteger; BigInteger java.math import java.nio.Buffer; Buffer java.nio import java.util.*; util.* java class Main {
    public static void main(String[] args){
        try {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String[] ss=br.readLine().split(" ");
            long a=Long.parseLong(ss[0]);
            long b=Long.parseLong(ss[1]);
            long c=Long.parseLong(ss[2]);
            long k=Long.parseLong(ss[3]);
            if(k<=a+b){
                System.out.println(Math.min(k,a));
            }else{
                k-=(a+b);
                System.out.println(a-k);
            }
        } catch (Exception e) {
            System.out.println("kkkk "+ e.getMessage());
        }
    }
    static void dfs(ArrayList<HashSet<Integer>> list,boolean[] vis,int i){
        vis[i]=true;
        Iterator<Integer> it=list.get(i).iterator();
        while(it.hasNext()){
            int u=it.next();
            if(!vis[u]){
                dfs(list,vis,u);
            }
        }
    }
    static int modInverse(int x,int m){
        return power(x,m-2,m);
    }
    static int power(int x, int y, int m)
    {
        if (y == 0)
            return 1;

        long p = power(x, y / 2, m) % m;
        p = (p * p) % m;

        return (int)((y%2==0)? p : (x*p)%m);
    }
    static void shuffle(String[] a) {
        Random get = new Random();
        for (int i = 0; i < a.length; i++) {
            int r = get.nextInt(a.length);
            String temp = a[i];
            a[i] = a[r];
            a[r] = temp;
        }
    }
    static class quad{
        int a,b,c,d;
        public quad(int a,int b,int c,int d){
            this.a=a;
            this.b=b;
            this.c=c;
            this.d=d;
        }
    }
} Main public static void main(String[] args){
        try {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String[] ss=br.readLine().split(" ");
            long a=Long.parseLong(ss[0]);
            long b=Long.parseLong(ss[1]);
            long c=Long.parseLong(ss[2]);
            long k=Long.parseLong(ss[3]);
            if(k<=a+b){
                System.out.println(Math.min(k,a));
            }else{
                k-=(a+b);
                System.out.println(a-k);
            }
        } catch (Exception e) {
            System.out.println("kkkk "+ e.getMessage());
        }
    } main {
        try {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String[] ss=br.readLine().split(" ");
            long a=Long.parseLong(ss[0]);
            long b=Long.parseLong(ss[1]);
            long c=Long.parseLong(ss[2]);
            long k=Long.parseLong(ss[3]);
            if(k<=a+b){
                System.out.println(Math.min(k,a));
            }else{
                k-=(a+b);
                System.out.println(a-k);
            }
        } catch (Exception e) {
            System.out.println("kkkk "+ e.getMessage());
        }
    } try {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String[] ss=br.readLine().split(" ");
            long a=Long.parseLong(ss[0]);
            long b=Long.parseLong(ss[1]);
            long c=Long.parseLong(ss[2]);
            long k=Long.parseLong(ss[3]);
            if(k<=a+b){
                System.out.println(Math.min(k,a));
            }else{
                k-=(a+b);
                System.out.println(a-k);
            }
        } catch (Exception e) {
            System.out.println("kkkk "+ e.getMessage());
        } catch (Exception e) {
            System.out.println("kkkk "+ e.getMessage());
        } Exception e {
            System.out.println("kkkk "+ e.getMessage());
        } System.out.println("kkkk "+ e.getMessage()) System.out.println System.out System System.out "kkkk "+ e.getMessage() "kkkk " e.getMessage() e.getMessage e {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String[] ss=br.readLine().split(" ");
            long a=Long.parseLong(ss[0]);
            long b=Long.parseLong(ss[1]);
            long c=Long.parseLong(ss[2]);
            long k=Long.parseLong(ss[3]);
            if(k<=a+b){
                System.out.println(Math.min(k,a));
            }else{
                k-=(a+b);
                System.out.println(a-k);
            }
        } BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] ss=br.readLine().split(" "); ss br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " long a=Long.parseLong(ss[0]); a Long.parseLong(ss[0]) Long.parseLong Long ss[0] ss 0 long b=Long.parseLong(ss[1]); b Long.parseLong(ss[1]) Long.parseLong Long ss[1] ss 1 long c=Long.parseLong(ss[2]); c Long.parseLong(ss[2]) Long.parseLong Long ss[2] ss 2 long k=Long.parseLong(ss[3]); k Long.parseLong(ss[3]) Long.parseLong Long ss[3] ss 3 if(k<=a+b){
                System.out.println(Math.min(k,a));
            }else{
                k-=(a+b);
                System.out.println(a-k);
            } k<=a+b k a+b a b {
                System.out.println(Math.min(k,a));
            } System.out.println(Math.min(k,a)) System.out.println System.out System System.out Math.min(k,a) Math.min Math k a {
                k-=(a+b);
                System.out.println(a-k);
            } k-=(a+b) k (a+b) a b System.out.println(a-k) System.out.println System.out System System.out a-k a k String[] args args static void dfs(ArrayList<HashSet<Integer>> list,boolean[] vis,int i){
        vis[i]=true;
        Iterator<Integer> it=list.get(i).iterator();
        while(it.hasNext()){
            int u=it.next();
            if(!vis[u]){
                dfs(list,vis,u);
            }
        }
    } dfs {
        vis[i]=true;
        Iterator<Integer> it=list.get(i).iterator();
        while(it.hasNext()){
            int u=it.next();
            if(!vis[u]){
                dfs(list,vis,u);
            }
        }
    } vis[i]=true vis[i] vis i true Iterator<Integer> it=list.get(i).iterator(); it list.get(i).iterator() list.get(i).iterator list.get(i) list.get list i while(it.hasNext()){
            int u=it.next();
            if(!vis[u]){
                dfs(list,vis,u);
            }
        } it.hasNext() it.hasNext it {
            int u=it.next();
            if(!vis[u]){
                dfs(list,vis,u);
            }
        } int u=it.next(); u it.next() it.next it if(!vis[u]){
                dfs(list,vis,u);
            } !vis[u] vis[u] vis u {
                dfs(list,vis,u);
            } dfs(list,vis,u) dfs list vis u ArrayList<HashSet<Integer>> list list boolean[] vis vis int i i static int modInverse(int x,int m){
        return power(x,m-2,m);
    } modInverse {
        return power(x,m-2,m);
    } return power(x,m-2,m); power(x,m-2,m) power x m-2 m 2 m int x x int m m static int power(int x, int y, int m)
    {
        if (y == 0)
            return 1;

        long p = power(x, y / 2, m) % m;
        p = (p * p) % m;

        return (int)((y%2==0)? p : (x*p)%m);
    } power {
        if (y == 0)
            return 1;

        long p = power(x, y / 2, m) % m;
        p = (p * p) % m;

        return (int)((y%2==0)? p : (x*p)%m);
    } if (y == 0)
            return 1; y == 0 y 0 return 1; 1 long p = power(x, y / 2, m) % m; p power(x, y / 2, m) % m power(x, y / 2, m) power x y / 2 y 2 m m p = (p * p) % m p (p * p) % m (p * p) p p m return (int)((y%2==0)? p : (x*p)%m); (int)((y%2==0)? p : (x*p)%m) (y%2==0) y%2 y 2 0 p (x*p)%m (x*p) x p m int x x int y y int m m static void shuffle(String[] a) {
        Random get = new Random();
        for (int i = 0; i < a.length; i++) {
            int r = get.nextInt(a.length);
            String temp = a[i];
            a[i] = a[r];
            a[r] = temp;
        }
    } shuffle {
        Random get = new Random();
        for (int i = 0; i < a.length; i++) {
            int r = get.nextInt(a.length);
            String temp = a[i];
            a[i] = a[r];
            a[r] = temp;
        }
    } Random get = new Random(); get new Random() for (int i = 0; i < a.length; i++) {
            int r = get.nextInt(a.length);
            String temp = a[i];
            a[i] = a[r];
            a[r] = temp;
        } int i = 0; int i = 0; i 0 i < a.length i a.length a a.length i++ i++ i {
            int r = get.nextInt(a.length);
            String temp = a[i];
            a[i] = a[r];
            a[r] = temp;
        } {
            int r = get.nextInt(a.length);
            String temp = a[i];
            a[i] = a[r];
            a[r] = temp;
        } int r = get.nextInt(a.length); r get.nextInt(a.length) get.nextInt get a.length a a.length String temp = a[i]; temp a[i] a i a[i] = a[r] a[i] a i a[r] a r a[r] = temp a[r] a r temp String[] a a static class quad{
        int a,b,c,d;
        public quad(int a,int b,int c,int d){
            this.a=a;
            this.b=b;
            this.c=c;
            this.d=d;
        }
    } quad int a a b b c c d; d public quad(int a,int b,int c,int d){
            this.a=a;
            this.b=b;
            this.c=c;
            this.d=d;
        } quad {
            this.a=a;
            this.b=b;
            this.c=c;
            this.d=d;
        } this.a=a this.a this this.a a this.b=b this.b this this.b b this.c=c this.c this this.c c this.d=d this.d this this.d d int a a int b b int c c int d d
import javax.swing.text.html.HTMLDocument;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.nio.Buffer;
import java.util.*;

class Main {
    public static void main(String[] args){
        try {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String[] ss=br.readLine().split(" ");
            long a=Long.parseLong(ss[0]);
            long b=Long.parseLong(ss[1]);
            long c=Long.parseLong(ss[2]);
            long k=Long.parseLong(ss[3]);
            if(k<=a+b){
                System.out.println(Math.min(k,a));
            }else{
                k-=(a+b);
                System.out.println(a-k);
            }
        } catch (Exception e) {
            System.out.println("kkkk "+ e.getMessage());
        }
    }
    static void dfs(ArrayList<HashSet<Integer>> list,boolean[] vis,int i){
        vis[i]=true;
        Iterator<Integer> it=list.get(i).iterator();
        while(it.hasNext()){
            int u=it.next();
            if(!vis[u]){
                dfs(list,vis,u);
            }
        }
    }
    static int modInverse(int x,int m){
        return power(x,m-2,m);
    }
    static int power(int x, int y, int m)
    {
        if (y == 0)
            return 1;

        long p = power(x, y / 2, m) % m;
        p = (p * p) % m;

        return (int)((y%2==0)? p : (x*p)%m);
    }
    static void shuffle(String[] a) {
        Random get = new Random();
        for (int i = 0; i < a.length; i++) {
            int r = get.nextInt(a.length);
            String temp = a[i];
            a[i] = a[r];
            a[r] = temp;
        }
    }
    static class quad{
        int a,b,c,d;
        public quad(int a,int b,int c,int d){
            this.a=a;
            this.b=b;
            this.c=c;
            this.d=d;
        }
    }
}