// Arup Guha
// 5/11/2016
// Solution to USACO 2016 Silver February Problem: Milk Pails

import java.util.*;
import java.io.*;

public class pails {

	final public static int IMPOSSIBLE = 1000;

	public static void main(String[] args) throws Exception {

		// Read in data.
		BufferedReader stdin = new BufferedReader(new FileReader("pails.in"));
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		int a = Integer.parseInt(tok.nextToken());
		int b = Integer.parseInt(tok.nextToken());
		int maxOps = Integer.parseInt(tok.nextToken());
		int total = Integer.parseInt(tok.nextToken());

		// Set up BFS.
		int res = Integer.MAX_VALUE;
		int[] dist = new int[200*200];
		Arrays.fill(dist, IMPOSSIBLE);
		LinkedList<Integer> q = new LinkedList<Integer>();
		q.offer(0);
		dist[0] = 0;

		// Run BFS.
		while (q.size() > 0) {

			// Get next item.
			int next = q.poll();
			int prevDist = dist[next];
			int first = next/200;
			int second = next%200;

			// Done with BFS.
			if (dist[next] > maxOps) break;

			// See if this one's better.
			res = Math.min(res, Math.abs(total-(first+second)));

			// Try filling the left.
			int fillLeft = 200*a + second;
			if (dist[fillLeft] == IMPOSSIBLE) {
				q.offer(fillLeft);
				dist[fillLeft] = prevDist + 1;
			}

			// Or right.
			int fillRight = 200*first + b;
			if (dist[fillRight] == IMPOSSIBLE) {
				q.offer(fillRight);
				dist[fillRight] = prevDist + 1;
			}

			// Empty left.
			if (dist[second] == IMPOSSIBLE) {
				q.offer(second);
				dist[second] = prevDist + 1;
			}

			// Empty right.
			if (dist[200*first] == IMPOSSIBLE) {
				q.offer(200*first);
				dist[200*first] = prevDist + 1;
			}

			// Pour left to right.
			int pour = Math.min(first, b-second);
			int pourLeft = 200*(first-pour) + (second+pour);
			if (dist[pourLeft] == IMPOSSIBLE) {
				q.offer(pourLeft);
				dist[pourLeft] = prevDist + 1;
			}

			// Pour right to left.
			pour = Math.min(second, a-first);
			int pourRight = 200*(first+pour) + (second-pour);
			if (dist[pourRight] == IMPOSSIBLE) {
				q.offer(pourRight);
				dist[pourRight] = prevDist + 1;
			}
		}

		// Write result.
		PrintWriter out = new PrintWriter(new FileWriter("pails.out"));
		out.println(res);
		out.close();
		stdin.close();
	}
}