using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Diagnostics;
using System.Globalization;

namespace CodeForces {
    struct Node {
        public int X, Y, D;
        public Node(int x,int y, int d) {
            X = x;
            Y = y;
            D = d;
        }
    }

    class Cf {
        static TextReader input;
        static void Main(string[] args) {
#if TESTS
            Stopwatch sw = new Stopwatch();
            input = new StreamReader("input.txt");
#else
            input = Console.In;
#endif
            Solve();
#if TESTS
            Console.WriteLine();
            Console.WriteLine("Milliseconds: {0}", sw.ElapsedMilliseconds);
            Console.ReadLine();
#endif
        }
        const int MOD = 1000000007;
        static int n, m, k;
        static void Solve() {
            string s = ReadLine();
            string t = ReadLine();
            k = ReadInt();
            n = s.Length;
            ulong a = 0;
            for (int i = 0; i < n; i++) {
                string s1 = s.Substring(0, i);
                string s2 = s.Substring(i);
                if (s2 + s1 == t)
                    ++a;
            }
            ulong b = (ulong)n - a;
            ulong[] da = new ulong[k+1];
            ulong[] db = new ulong[k+1];
            if (s == t)
                da[0] = 1;
            else
                db[0] = 1;
            for (int i = 1; i <= k; i++) {
                da[i] = (da[i - 1] * (a - 1) + db[i - 1] * a) % MOD;
                db[i] = (da[i - 1] * b + db[i - 1] * (b - 1)) % MOD;
            }
            Console.WriteLine(da[k]);
        }

        static int[] Z_function(string s) {
            int n = s.Length;
            int[] z = new int[n];
            for (int i = 1, l = 0, r = 0; i < n; ++i) {
                if (i <= r)
                    z[i] = Math.Min(r - i + 1, z[i - l]);
                while (i + z[i] < n && s[z[i]] == s[i + z[i]])
                    ++z[i];
                if (i + z[i] - 1 > r) {
                    l = i; r = i + z[i] - 1;
                }
            }
            return z;
        }

        static int SquaredDistanceBetweenSegments(int x1, int y1, int x2, int y2, int xx1, int yy1, int xx2, int yy2) {
            return Math.Min(Math.Min(SquaredDistanceToSegment(x1, y1, xx1, yy1, xx2, yy2), SquaredDistanceToSegment(x2, y2, xx1, yy1, xx2, yy2)),
                Math.Min(SquaredDistanceToSegment(xx1, yy1, x1, y1, x2, y2), SquaredDistanceToSegment(xx2, yy2, x1, y1, x2, y2)));
        }

        static int SquaredDistanceToSegment(int x, int y, int x1, int y1, int x2, int y2) {
            int result = Math.Min(SquaredDistance(x, y, x1, y1), SquaredDistance(x, y, x2, y2));
            if (x1 == x2 && y1 <= y && y <= y2)
                result = (x - x1) * (x - x1);
            else if (y1 == y2 && x1 <= x && x <= x2)
                result = (y - y1) * (y - y1);
            return result;
        }

        static int SquaredDistance(int ax, int ay, int bx, int by) {
            return (ax - bx) * (ax - bx) + (ay - by) * (ay - by);
        }

        #region read helpers
        public static List<int> ReadIntList() {
            return input.ReadLine().Split(' ').Select(x => int.Parse(x)).ToList();
        }

        public static int[] ReadIntArray() {
            return input.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
        }

        public static string ReadLine() {
            return input.ReadLine();
        }

        public static int ReadInt() {
            return int.Parse(input.ReadLine());
        }
#endregion
    }

    public class SegmentTree {
        private int[] a, t;
        private int n;
        public SegmentTree(int[] a) {
            n = a.Length;
            this.a = a;
            t = new int[n << 2];
            buildSegmentTree(0, n - 1, 1);
        }

        private void buildSegmentTree(int l, int r, int v) {
            if (l == r)
                t[v] = a[l];
            else {
                int m = (l + r) >> 1;
                int vl = v << 1;
                int vr = (v << 1) | 1;
                buildSegmentTree(l, m, vl);
                buildSegmentTree(m + 1, r, vr);
                t[v] = t[vl] + t[vr];
            }
        }

        public int QuerySegmentTree(int l, int r) {
            return querySegmentTree(1, l, r, 0, n - 1);
        }

        public void Update(int v, int tl, int tr, int pos, int new_val) {
            if (tl == tr)
                t[v] = new_val;
            else {
                int tm = (tl + tr) / 2;
                if (pos <= tm)
                    Update(v << 1, tl, tm, pos, new_val);
                else
                    Update(v << 1 + 1, tm + 1, tr, pos, new_val);
                t[v] = t[v << 1] + t[v << 1 + 1];
            }
        }

        private int querySegmentTree(int v, int l, int r, int tl, int tr) {
            if (l > r)
                return 0;
            if (l == tl && r == tr)
                return t[v];
            int m = (tl + tr) >> 1;
            return querySegmentTree(v << 1, l, Math.Min(r, m), tl, m) + querySegmentTree((v << 1) | 1, Math.Max(l, m + 1), r, m + 1, tr);
        }
    }
    class Graph : List<List<int>> {
        public Graph(int num): base(num) {
            for (int i = 0; i < num; i++) {
                this.Add(new List<int>());
            }
        }
    }
    class Heap<T> : IEnumerable<T> where T : IComparable<T> {
        private List<T> heap = new List<T>();
        private int heapSize;
        private int Parent(int index) {
            return (index - 1) >> 1;
        }
        public int Count {
            get { return heap.Count; }
        }
        private int Left(int index) {
            return (index << 1) | 1;
        }
        private int Right(int index) {
            return (index << 1) + 2;
        }
        private void Max_Heapify(int i) {
            int l = Left(i);
            int r = Right(i);
            int largest = i;
            if (l < heapSize && heap[l].CompareTo(heap[i]) > 0)
                largest = l;
            if (r < heapSize && heap[r].CompareTo(heap[largest]) > 0)
                largest = r;
            if (largest != i) {
                T temp = heap[largest];
                heap[largest] = heap[i];
                heap[i] = temp;
                Max_Heapify(largest);
            }
        }
        private void BuildMaxHeap() {
            for (int i = heap.Count >> 1; i >= 0; --i)
                Max_Heapify(i);
        }

        public IEnumerator<T> GetEnumerator() {
            return heap.GetEnumerator();
        }

        public void Sort() {
            for (int i = heap.Count - 1; i > 0; --i) {
                T temp = heap[i];
                heap[i] = heap[0];
                heap[0] = temp;
                --heapSize;
                Max_Heapify(0);
            }
        }

        public T Heap_Extract_Max() {
            T max = heap[0];
            heap[0] = heap[--heapSize];
            Max_Heapify(0);
            return max;
        }

        public void Clear() {
            heap.Clear();
            heapSize = 0;
        }

        public void Insert(T item) {
            if (heapSize < heap.Count)
                heap[heapSize] = item;
            else
                heap.Add(item);
            int i = heapSize;
            while (i > 0 && heap[Parent(i)].CompareTo(heap[i]) < 0) {
                T temp = heap[i];
                heap[i] = heap[Parent(i)];
                heap[Parent(i)] = temp;
                i = Parent(i);
            }
            ++heapSize;
        }

        IEnumerator IEnumerable.GetEnumerator() {
            return ((IEnumerable)heap).GetEnumerator();
        }
    }
    public class Treap {
        private static Random rand = new Random();
        public static Treap Merge(Treap l, Treap r) {
            if (l == null) return r;
            if (r == null) return l;
            Treap res;
            if (l.y > r.y) {
                Treap newR = Merge(l.right, r);
                res = new Treap(l.x, l.y, l.left, newR);
            }
            else {
                Treap newL = Merge(l, r.left);
                res = new Treap(r.x, r.y, newL, r.right);
            }
            return res;
        }

        public void Split(int x, out Treap l, out Treap r) {
            Treap newTree = null;
            if (this.x <= x) {
                if (right == null)
                    r = null;
                else
                    right.Split(x, out newTree, out r);
                l = new Treap(this.x, y, left, newTree);
            }
            else {
                if (left == null)
                    l = null;
                else
                    left.Split(x, out l, out newTree);
                r = new Treap(this.x, y, newTree, right);
            }
        }

        public Treap Add(int x) {
            Treap l, r;
            Split(x, out l, out r);
            Treap m = new Treap(x, rand.Next());
            return Merge(Merge(l, m), r);
        }

        public Treap Remove(int x) {
            Treap l, m, r;
            Split(x - 1, out l, out r);
            r.Split(x, out m, out r);
            return Merge(l, r);
        }

        private int x, y, cost, maxTreeCost;
        private Treap left, right;
        public Treap(int x, int y, Treap l, Treap r) {
            this.x = x;
            this.y = y;
            left = l;
            right = r;
        }
        public Treap(int x, int y) : this(x, y, null, null) { }
        public void InOrder() {
            inOrder(this);
        }
        private void inOrder(Treap t) {
            if (t == null) return;
            inOrder(t.left);
            Console.WriteLine(t.x);
            inOrder(t.right);
        }
    }
    public static class Extensions {
        public static void Fill(this int[] array, int val) {
            for (int i = 0; i < array.Length; i++) {
                array[i] = val;
            }
        }

        public static void Fill(this double[] array, double val) {
            for (int i = 0; i < array.Length; i++) {
                array[i] = val;
            }
        }

        public static int ToInt(this string s) {
            return int.Parse(s);
        }

        public static string Fill(this char c, int count) {
            char[] r = new char[count];
            for (int i = 0; i < count; ++i)
                r[i] = c;
            return new string(r);
        }

        public static void Print<T>(this IEnumerable<T> arr) {
            foreach (T t in arr)
                Console.Write("{0} ", t);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
             string[]y=Console.ReadLine().Split(' ');
             int[]a=new int[3];
             a[0]=int.Parse(y[0]);
             a[1]=int.Parse(y[1]);
             a[2]=int.Parse(y[2]);
             int n=int.Parse(y[3]);
             Array.Sort(a);
             int o1=a[1]-n;
             int o2=a[1]+n;
             int t=0;
             if(a[0]>o1)
             t+=Math.Abs(a[0]-o1);
             if(a[2]<o2)
             t+=Math.Abs(a[2]-o2);

             Console.WriteLine(t);

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp41
{
    class Program
    {
        static void Main(string[] args)
        {
            var position = Array.ConvertAll(Console.ReadLine().Trim().Split(), long.Parse);
            long a = position[0];
            long b = position[1];
            long c = position[2];
            long d = position[3];

            long[] mas = new long[3];
            mas[0] = a;
            mas[1] = b;
            mas[2] = c;
            Array.Sort(mas);
            long count=0;
            if (d - (mas[2] - mas[1]) > 0)
                count = d - (mas[2] - mas[1]);
            if(d-(mas[1]-mas[0])>0)
                count += d - (mas[1] - mas[0]);
            Console.WriteLine(count);
        }
    }
}


using System;
using System.Collections.Generic;

namespace olymp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split();
            int a = Int32.Parse(input[0]);
            int b = Int32.Parse(input[1]);
            int c = Int32.Parse(input[2]);
            int first = 0, second = 0, third = 0, time = 0;
            int d = Int32.Parse(input[3]);
            int[] order = new int[]{a,b,c};
            Array.Sort(order);
            first = order[0];
            second = order[1];
            third = order[2];
            if(second - first < d)
            {
                time += first + d - second;
            }
            if(third - second < d)
            {
                time += second + d - third;
            }
            Console.WriteLine(time.ToString());
        }

    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Startup
{
    static void Main()
    {
        List<int> firstLine = ReadLineAndParseToList();

        int distance = firstLine[3];

        int firstPosition = firstLine[0];
        int secondPosition = firstLine[1];
        int thirdPosition = firstLine[2];

        int result = 0;
        result += CalculateSeconds(firstPosition, secondPosition, thirdPosition, distance);

        if (result == 0)
        {
            result += CalculateSeconds(secondPosition, firstPosition, thirdPosition, distance);
        }

        if (result == 0)
        {
            result += CalculateSeconds(thirdPosition, secondPosition, firstPosition, distance);
        }

        Console.WriteLine(result);
    }

    public static int CalculateSeconds(int firstPosition, int secondPosition, int thirdPosition, int distance)
    {
        int secondsNeeded = 0;

        if (firstPosition >= secondPosition && firstPosition >= thirdPosition)
        {
            if (secondPosition >= thirdPosition)
            {
                if (distance > firstPosition - secondPosition)
                {
                    secondsNeeded = distance - (firstPosition - secondPosition);
                }
                if (distance > secondPosition - thirdPosition)
                {
                    secondsNeeded += distance - (secondPosition - thirdPosition);
                }
            }
            else
            {
                if (distance > firstPosition - thirdPosition)
                {
                    secondsNeeded = distance - (firstPosition - thirdPosition);
                }
                if (distance > thirdPosition - secondPosition)
                {
                    secondsNeeded += distance - (thirdPosition - secondPosition);
                }
            }
        }
        return secondsNeeded;
    }

    public static List<int> ReadLineAndParseToList()
    {
        return Console.ReadLine().Split().Select(int.Parse).ToList();
    }

    static int[] ReadLineAndParseToArray()
    {
        return Console.ReadLine().Split().Select(int.Parse).ToArray();
    }

    public static int[,] TransposeMaxtrix(int[,] matrix)
    {
        int w = matrix.GetLength(0);
        int h = matrix.GetLength(1);

        int[,] result = new int[h, w];

        for (int i = 0; i < w; i++)
        {
            for (int j = 0; j < h; j++)
            {
                result[j, i] = matrix[i, j];
            }
        }
        return result;
    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;

namespace Olymp
{
  public class ProblemSolver
  {
    private readonly Tokenizer input;

    public void Solve()
    {
      var a = input.ReadIntArray(3);
      var d = input.ReadInt();
      Array.Sort(a);
      Write(Max(0, d - (a[1] - a[0]) + Max(0, d - (a[2] - a[1]))));
    }

    public ProblemSolver(TextReader input)
    {
      this.input = new Tokenizer(input);
    }
  }

  #region Service classes

  public class Tokenizer
  {
    private readonly TextReader reader;

    public string ReadToEnd()
    {
      return reader.ReadToEnd();
    }

    public int ReadInt()
    {
      var c = SkipWS();
      if (c == -1)
        throw new EndOfStreamException();
      var isNegative = false;
      if (c == '-' || c == '+')
      {
        isNegative = c == '-';
        c = reader.Read();
        if (c == -1)
          throw new EndOfStreamException("Digit expected, but end of stream occurs");
      }

      if (!char.IsDigit((char)c))
        throw new InvalidOperationException($"Digit expected, but was: '{(char)c}'");
      var result = (char)c - '0';
      c = reader.Read();
      while (c > 0 && !char.IsWhiteSpace((char)c))
      {
        if (!char.IsDigit((char)c))
          throw new InvalidOperationException($"Digit expected, but was: '{(char)c}'");
        result = result * 10 + (char)c - '0';
        c = reader.Read();
      }

      if (isNegative)
        result = -result;
      return result;
    }

    public string ReadLine()
    {
      return reader.ReadLine();
    }

    public long ReadLong()
    {
      return long.Parse(ReadToken());
    }

    public double ReadDouble()
    {
      return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
    }

    public int[] ReadIntArray(int n)
    {
      var a = new int[n];
      for (var i = 0; i < n; i++)
        a[i] = ReadInt();
      return a;
    }

    public (int, int) Read2Int() =>
      (ReadInt(), ReadInt());

    public (int, int, int) Read3Int() =>
      (ReadInt(), ReadInt(), ReadInt());

    public (int, int, int, int) Read4Int() =>
      (ReadInt(), ReadInt(), ReadInt(), ReadInt());

    public long[] ReadLongArray(int n)
    {
      var a = new long[n];
      for (var i = 0; i < n; i++)
        a[i] = ReadLong();
      return a;
    }

    public double[] ReadDoubleArray(int n)
    {
      var a = new double[n];
      for (var i = 0; i < n; i++)
        a[i] = ReadDouble();
      return a;
    }

    public string ReadToken()
    {
      var c = SkipWS();
      if (c == -1)
        return null;
      var sb = new StringBuilder();
      while (c > 0 && !char.IsWhiteSpace((char)c))
      {
        sb.Append((char)c);
        c = reader.Read();
      }

      return sb.ToString();
    }

    private int SkipWS()
    {
      var c = reader.Read();
      if (c == -1)
        return c;
      while (c > 0 && char.IsWhiteSpace((char)c))
        c = reader.Read();
      return c;
    }

    public Tokenizer(TextReader reader)
    {
      this.reader = reader;
    }
  }

  internal class Program
  {
    public static void Main()
    {
      var solver = new ProblemSolver(In);
      solver.Solve();
    }
  }

  #endregion
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;

namespace ConsoleApp1
{
    // 1185/problem/A
    public class Program
    {
        static void Main(string[] args)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture =
               System.Globalization.CultureInfo.InvariantCulture;
#if !ONLINE_JUDGE
            Console.SetIn(File.OpenText("../../input.txt"));
            FileStream writerOutput = new FileStream("../../output.txt", FileMode.Truncate, FileAccess.Write);
            StreamWriter writer = new StreamWriter(writerOutput);
            writer.AutoFlush = true;
            //Console.SetOut(writer);
#endif
            string s = Console.ReadLine();
            string[] sAr = s.Split(' ');
            int a,b,c,d;
            a = int.Parse(sAr[0]);
            b = int.Parse(sAr[1]);
            c = int.Parse(sAr[2]);
            d = int.Parse(sAr[3]);
            int[] p = new int[] { a, b, c };
            var pSort = p.OrderBy(i => i).ToList();
            a = pSort[0];
            b = pSort[1];
            c = pSort[2];
            long ans = 0;
            long ab = Math.Abs(a - b), bc = Math.Abs(b-c);
            if (ab < d)
                ans += d - ab;
            if (bc < d)
                ans += d - bc;
            Console.WriteLine(ans);
        }
    }
}


// Problem: 1185A - Ropewalkers
// Author: Gusztav Szmolik

using System;

class Ropewalkers
    {
    public static int Main ()
        {
        string line = Console.ReadLine ();
        if (line == null)
            return -1;
        char[] delims = new char[] {' ', '\t'};
        string[] words = line.Split (delims,StringSplitOptions.RemoveEmptyEntries);
        if (words.Length != 4)
            return -1;
        uint a;
        if (!ReadInputNumber (words[0], out a))
            return -1;
        uint b;
        if (!ReadInputNumber (words[1], out b))
            return -1;
        uint c;
        if (!ReadInputNumber (words[2], out c))
            return -1;
        uint d;
        if (!ReadInputNumber (words[3], out d))
            return -1;
        uint[] initPositions = new uint[] {a,b,c};
        Array.Sort (initPositions);
        uint ans = 0;
        uint dist1 = initPositions[1]-initPositions[0];
        uint dist2 = initPositions[2]-initPositions[1];
        if (dist1 < d)
            ans += d-dist1;
        if (dist2 < d)
            ans += d-dist2;
        Console.WriteLine (ans);
        return 0;
        }

    static bool ReadInputNumber (string s, out uint num)
        {
        if (!UInt32.TryParse (s, out num))
            return false;
        if (num < 1 || num > 1000000000)
            return false;
        return true;
        }
    }


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using static System.Math;
using System.Text;

namespace Program
{
    public static class CODEFORCES1
    {
        static public void Solve()
        {
            var param = Sort(NextIntList(3));
            var d = NextInt;
            Console.WriteLine(Max(0, d - param[1] + param[0]) + Max(0, d - param[2] + param[1]));

        }

        static public void Main(string[] args)
        {
            if (args.Length == 0)
            {
                var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
                Console.SetOut(sw);
            }
            Solve();
            Console.Out.Flush();
        }
        static Random rand = new Random();
        static class Console_
        {
            private static Queue<string> param = new Queue<string>();
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public static string NextString()
            {
                if (param.Count == 0) foreach (var item in Console.ReadLine().Split(' ')) param.Enqueue(item);
                return param.Dequeue();
            }
        }
        static int NextInt => int.Parse(Console_.NextString());
        static long NextLong => long.Parse(Console_.NextString());
        static double NextDouble => double.Parse(Console_.NextString());
        static string NextString => Console_.NextString();
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static List<int> NextIntList(long N) => Enumerable.Repeat(0, (int)N).Select(_ => NextInt).ToList();
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static List<long> NextLongList(long N) => Enumerable.Repeat(0, (int)N).Select(_ => NextLong).ToList();
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static List<double> NextDoubleList(long N) => Enumerable.Repeat(0, (int)N).Select(_ => NextDouble).ToList();
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static List<string> NextStringList(long N) => Enumerable.Repeat(0, (int)N).Select(_ => NextString).ToList();
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static IEnumerable<T> OrderByRand<T>(this IEnumerable<T> x) => x.OrderBy(_ => rand.Next());
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static IEnumerable<T> Repeat<T>(T v, long n) => Enumerable.Repeat<T>(v, (int)n);
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static List<T> Sort<T>(List<T> l) where T : IComparable
        {
            var tmp = l.ToArray(); Array.Sort(tmp); return tmp.ToList();
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static List<T> Sort<T>(List<T> l, Comparison<T> comp) where T : IComparable
        {
            var tmp = l.ToArray(); Array.Sort(tmp, comp); return tmp.ToList();
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static List<T> RevSort<T>(List<T> l) where T : IComparable
        {
            var tmp = l.ToArray(); Array.Sort(tmp, (x, y) => y.CompareTo(x)); return tmp.ToList();
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static List<T> RevSort<T>(List<T> l, Comparison<T> comp) where T : IComparable
        {
            var tmp = l.ToArray(); Array.Sort(tmp, (x, y) => comp(y, x)); return tmp.ToList();
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static IEnumerable<long> Prime(long x)
        {
            if (x < 2) yield break;
            yield return 2;
            var halfx = x / 2;
            var table = new bool[halfx + 1];
            var max = (long)(Math.Sqrt(x) / 2);
            for (long i = 1; i <= max; ++i)
            {
                if (table[i]) continue;
                var add = 2 * i + 1;
                yield return add;
                for (long j = 2 * i * (i + 1); j <= halfx; j += add) table[j] = true;
            }
            for (long i = max + 1; i <= halfx; ++i) if (!table[i] && 2 * i + 1 <= x) yield return 2 * i + 1;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static IEnumerable<long> Divisor(long x)
        {
            if (x < 1) yield break;
            var max = (long)Math.Sqrt(x);
            for (long i = 1; i <= max; ++i)
            {
                if (x % i != 0) continue;
                yield return i;
                if (i != x / i) yield return x / i;
            }
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static long GCD(long a, long b)
        {
            while (b > 0) { var tmp = b; b = a % b; a = tmp; }
            return a;
        }
        class PQ<T> where T : IComparable
        {
            private List<T> h;
            private Comparison<T> c;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public PQ(int cap, Comparison<T> c, bool asc = true) { h = new List<T>(cap); this.c = asc ? c : (x, y) => c(y, x); }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public PQ(Comparison<T> c, bool asc = true) { h = new List<T>(); this.c = asc ? c : (x, y) => c(y, x); }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public PQ(int cap, bool asc = true) : this(cap, (x, y) => x.CompareTo(y), asc) { }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public PQ(bool asc = true) : this((x, y) => x.CompareTo(y), asc) { }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public void Push(T v)
            {
                var i = h.Count;
                h.Add(v);
                while (i > 0)
                {
                    var ni = (i - 1) / 2;
                    if (c(v, h[ni]) >= 0) break;
                    h[i] = h[ni]; i = ni;
                }
                h[i] = v;
            }
            public T Peek => h[0];
            public int Count => h.Count;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public T Pop()
            {
                var r = h[0];
                var v = h[h.Count - 1];
                h.RemoveAt(h.Count - 1);
                if (h.Count == 0) return r;
                var i = 0;
                while (i * 2 + 1 < h.Count)
                {
                    var i1 = i * 2 + 1;
                    var i2 = i * 2 + 2;
                    if (i2 < h.Count && c(h[i1], h[i2]) > 0) i1 = i2;
                    if (c(v, h[i1]) <= 0) break;
                    h[i] = h[i1]; i = i1;
                }
                h[i] = v;
                return r;
            }
        }
        class PQ<TKey, TValue> where TKey : IComparable
        {
            private PQ<Tuple<TKey, TValue>> q;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public PQ(int cap, Comparison<TKey> c, bool asc = true) { q = new PQ<Tuple<TKey, TValue>>(cap, (x, y) => c(x.Item1, y.Item1), asc); }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public PQ(Comparison<TKey> c, bool asc = true) { q = new PQ<Tuple<TKey, TValue>>((x, y) => c(x.Item1, y.Item1), asc); }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public PQ(int cap, bool asc = true) : this(cap, (x, y) => x.CompareTo(y), asc) { }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public PQ(bool asc = true) : this((x, y) => x.CompareTo(y), asc) { }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public void Push(TKey k, TValue v) => q.Push(Tuple.Create(k, v));
            public Tuple<TKey, TValue> Peek => q.Peek;
            public int Count => q.Count;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public Tuple<TKey, TValue> Pop() => q.Pop();
        }
        class Mod
        {
            static public long _mod = 1000000007;
            private long _val = 0;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public Mod() { }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public Mod(long x) { if (x < _mod && x >= 0) _val = x; else if ((_val = x % _mod) < 0) _val += _mod; }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public implicit operator Mod(long x) => new Mod(x);
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public implicit operator long(Mod x) => x._val;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public Mod operator +(Mod x, Mod y) { var t = x._val + y._val; return new Mod() { _val = t < _mod ? t : t - _mod }; }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public Mod operator +(Mod x, long y) => x._val + y;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public Mod operator +(long x, Mod y) => x + y._val;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public Mod operator -(Mod x, Mod y) { var t = x._val - y._val; return new Mod() { _val = t < _mod ? t : t - _mod }; }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public Mod operator -(Mod x, long y) => x._val - y;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public Mod operator -(long x, Mod y) => x - y._val;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public Mod operator /(Mod x, Mod y) => x._val * Inverse(y._val);
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public bool operator ==(Mod x, Mod y) => x._val == y._val;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public bool operator ==(Mod x, long y) => x._val == y;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public bool operator ==(long x, Mod y) => x == y._val;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public bool operator !=(Mod x, Mod y) => x._val != y._val;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public bool operator !=(Mod x, long y) => x._val != y;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public bool operator !=(long x, Mod y) => x != y._val;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public Mod Pow(Mod x, long y)
            {
                Mod a = 1;
                while (y != 0)
                {
                    if ((y & 1) == 1) a *= x;
                    x *= x; y >>= 1;
                }
                return a;
            }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public long Inverse(long x)
            {
                long b = _mod, r = 1, u = 0, t = 0;
                while (b > 0)
                {
                    var q = x / b;
                    t = u; u = r - q * u; r = t;
                    t = b; b = x - q * b; x = t;
                }
                return r < 0 ? r + _mod : r;
            }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            override public bool Equals(object obj) => obj == null ? false : _val == ((Mod)obj)._val;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public bool Equals(Mod obj) => obj == null ? false : _val == obj._val;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public override int GetHashCode() => _val.GetHashCode();
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public override string ToString() => _val.ToString();
            static private List<Mod> _fact = new List<Mod>();
            static private List<Mod> _ifact = new List<Mod>();
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static private void Build(int n)
            {
                if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i)
                        if (i == 0L) { _fact.Add(1); _ifact.Add(1); }
                        else { _fact.Add(_fact[i - 1] * i); _ifact.Add(_ifact[i - 1] * Mod.Pow(i, _mod - 2)); }
            }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public Mod Comb(int n, int k)
            {
                Build(n);
                if (n == 0 && k == 0) return 1;
                if (n < k || n < 0) return 0;
                return _ifact[n - k] * _ifact[k] * _fact[n];
            }
        }
        class Mat<T>
        {
            private T[,] m;
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public Mat(T[,] v) { m = (T[,])v.Clone(); }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public implicit operator Mat<T>(T[,] v) => new Mat<T>(v);
            public T this[int r, int c] { get { return m[r, c]; } set { m[r, c] = value; } }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public Mat<T> operator +(Mat<T> a, T x)
            {
                var tm = (T[,])a.m.Clone();
                for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] += (dynamic)x;
                return new Mat<T>(tm);
            }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public Mat<T> operator +(Mat<T> a, Mat<T> b)
            {
                var tm = (T[,])a.m.Clone();
                for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] += (dynamic)b[r, c];
                return new Mat<T>(tm);
            }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public Mat<T> operator -(Mat<T> a, T x)
            {
                var tm = (T[,])a.m.Clone();
                for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] -= (dynamic)x;
                return new Mat<T>(tm);
            }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public Mat<T> operator -(Mat<T> a, Mat<T> b)
            {
                var tm = (T[,])a.m.Clone();
                for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] -= (dynamic)b[r, c];
                return new Mat<T>(tm);
            }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public Mat<T> operator *(Mat<T> a, T x)
            {
                var tm = (T[,])a.m.Clone();
                for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] *= (dynamic)x;
                return new Mat<T>(tm);
            }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public Mat<T> operator *(Mat<T> a, Mat<T> b)
            {
                var nr = a.m.GetLength(0); var nc = b.m.GetLength(1);
                var tm = new T[nr, nc];
                for (int i = 0; i < nr; ++i) for (int j = 0; j < nc; ++j) tm[i, j] = (dynamic)0;
                for (int r = 0; r < nr; ++r) for (int c = 0; c < nc; ++c) for (int i = 0; i < a.m.GetLength(1); ++i) tm[r, c] += a[r, i] * (dynamic)b[i, c];
                return new Mat<T>(tm);
            }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static public Mat<T> Pow(Mat<T> x, long y)
            {
                var n = x.m.GetLength(0);
                var t = (Mat<T>)new T[n, n];
                for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) t[i, j] = (dynamic)(i == j ? 1 : 0);
                while (y != 0)
                {
                    if ((y & 1) == 1) t *= x;
                    x *= x; y >>= 1;
                }
                return t;
            }
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static Mat<T> Pow<T>(Mat<T> x, long y) => Mat<T>.Pow(x, y);
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static Mod Pow(Mod x, long y) => Mod.Pow(x, y);
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static long Pow(long x, long y)
        {
            long a = 1;
            while (y != 0)
            {
                if ((y & 1) == 1) a *= x;
                x *= x; y >>= 1;
            }
            return a;
        }
        static List<long> _fact = new List<long>();
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static void _Build(int n)
        {
            if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i)
                    if (i == 0L) _fact.Add(1);
                    else _fact.Add(_fact[i - 1] * i);
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static long Comb(int n, int k)
        {
            _Build(n);
            if (n == 0 && k == 0) return 1;
            if (n < k || n < 0) return 0;
            return _fact[n] / _fact[k] / _fact[n - k];
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Trenya190619
{
    class Input
    {
        private static IEnumerator<string> getin()
        {
            while (true)
                foreach (string s in Console.ReadLine().Split().Where(x => x.Length > 0))
                    yield return s;
        }

        private IEnumerator<string> inp = getin();

        public string GetString() { inp.MoveNext(); return inp.Current; }
        public int GetInt() { return int.Parse(GetString()); }
        public long GetLong() { return long.Parse(GetString()); }
        public double GetDouble() { return double.Parse(GetString()); }
    }

    class Program
    {
        static Input csin = new Input();

        static void Main(string[] args)
        {
            long[] r = new long[3];
            for (int i = 0; i < 3; i++) r[i] = csin.GetLong();

            long d = csin.GetLong();

            Array.Sort(r);

            long ans = d - Math.Min(d, r[1] - r[0]) + d - Math.Min(d, r[2] - r[1]);

            Console.WriteLine(ans);
            //Console.ReadKey();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace ProjectContest
{
    class Test
    {
        static void Main()
        {
            var v = Array.ConvertAll(Console.ReadLine().Split(), Convert.ToInt32).ToList();
            int d = v[3];
            v.RemoveAt(3);
            v.Sort();
            int a = v[0], b = v[1], c = v[2];
            int kq = 0;
            if (a > b - d) kq += (a + d - b);
            if (c < b + d) kq += (b + d - c);
            Console.WriteLine(kq);

        }
    }
}


using System;
using System.Collections.Generic;
class Program
{
	static void Main(string[] args)
	{
		string[] str = Console.ReadLine().Split();
		int[] A = new int[3];
		A[0] = int.Parse(str[0]);
		A[1] = int.Parse(str[1]);
		A[2] = int.Parse(str[2]);
		int D = int.Parse(str[3]);
		Array.Sort(A);
		int ans = Math.Max(D,A[1]-A[0])+Math.Max(D,A[2]-A[1]) - (A[2] - A[0]);
		Console.WriteLine(ans);
	}
}


using System;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var tokens = Console.ReadLine().Split();
            var array = new int[] { int.Parse(tokens[0]), int.Parse(tokens[1]), int.Parse(tokens[2]) };
            Array.Sort(array);
            var leftValue = array[1] - array[0];
            var rightValue = array[2] - array[1];
            var d = int.Parse(tokens[3]);

            var result = d > leftValue ? d - leftValue : 0;
            result += d > rightValue ? d - rightValue : 0;

            Console.WriteLine(result);
        }
    }
}


using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
    class Program
    {
        static int min(int a, int b)
        {
            if (a > b)
                return b;
            return a;
        }
       static int max(int a, int b)
        {
            if (a > b)
                return a;
            return b;
        }
        static void Main(string[] args)
        {
            int a,b,c,d;
           // String s;
            //s = Console.ReadLine();
            var t = Console.ReadLine().Split().ToArray();
            a = int.Parse(t[0]);
            b = int.Parse(t[1]);
            c = int.Parse(t[2]);
            d = int.Parse(t[3]);
            int x1,x2,x3;
            x1 = min(min(a,c), b);
            x3 = max(max(a, c), b);
            if (a!=x1 && a!=x3)
                x2 = a;
            else
                if (b != x1 && b != x3)
                x2 = b;
            else
                x2 = c;
            int sum = 0;
            if (x3 - x2 < d)
                sum += d - x3 + x2;
            if (x2 - x1 < d)
                sum += d - x2 + x1;
            Console.WriteLine(sum);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using SB = System.Text.StringBuilder;
//using System.Threading.Tasks;
//using System.Text.RegularExpressions;
//using System.Globalization;
//using System.Diagnostics;
using static System.Console;
using System.Numerics;
using static System.Math;
using pair = Pair<int, int>;

class Program
{
    static void Main()
    {
        //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().solve();
        Out.Flush();
    }
    readonly Scanner cin = new Scanner();
    readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑
    readonly int mod = 1000000007;
    readonly int dom = 998244353;
    bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
    bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (b.CompareTo(a) < 0) { a = b; return true; } return false; }

    void solve()
    {
        var X = new long[3];
        for (int i = 0; i < 3; i++)
        {
            X[i] = cin.nextint;
        }
        long D = cin.nextint;
        Array.Sort(X);

        long ans = Max(0, D - Abs(X[1] - X[2]));
        ans += Max(0, D - Abs(X[0] - X[1]));

        WriteLine(ans);

    }

}

static class Ex
{
    public static void join<T>(this IEnumerable<T> values, string sep = " ") => WriteLine(string.Join(sep, values));
    public static string concat<T>(this IEnumerable<T> values) => string.Concat(values);
    public static string reverse(this string s) { var t = s.ToCharArray(); Array.Reverse(t); return t.concat(); }

    public static int lower_bound<T>(this IList<T> arr, T val) where T : IComparable<T>
    {
        int low = 0, high = arr.Count;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (arr[mid].CompareTo(val) < 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
    public static int upper_bound<T>(this IList<T> arr, T val) where T : IComparable<T>
    {
        int low = 0, high = arr.Count;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (arr[mid].CompareTo(val) <= 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
}

class Pair<T, U> : IComparable<Pair<T, U>> where T : IComparable<T> where U : IComparable<U>
{
    public T f; public U s;
    public Pair(T f, U s) { this.f = f; this.s = s; }
    public int CompareTo(Pair<T, U> a) => f.CompareTo(a.f) != 0 ? f.CompareTo(a.f) : s.CompareTo(a.s);
    public override string ToString() => $"{f} {s}";
}

class Scanner
{
    string[] s; int i;
    readonly char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] scan => ReadLine().Split();
    public int[] scanint => Array.ConvertAll(scan, int.Parse);
    public long[] scanlong => Array.ConvertAll(scan, long.Parse);
    public double[] scandouble => Array.ConvertAll(scan, double.Parse);
    public string next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next;
        }
    }
    public int nextint => int.Parse(next);
    public long nextlong => long.Parse(next);
    public double nextdouble => double.Parse(next);
}


using System;
using System.Text;
using System.Collections.Generic;

namespace test_2
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            string[] strs = Console.ReadLine().Split();
            int[] arr = new int[4];
            for (int i = 0; i < 4; i++)
            {
                arr[i] = int.Parse(strs[i]);
            }

            int d = arr[3];
            int[] newArr = { arr[0], arr[1], arr[2] };
            Array.Sort(newArr);

            int sumSteps = 0;
            int delta1 = newArr[1] - newArr[0];
            int delta2 = newArr[2] - newArr[1];

            if (delta1 < d) { sumSteps += d - delta1; }
            if (delta2 < d) { sumSteps += d - delta2; }

            Console.WriteLine(sumSteps);
        }
    }
}


using System;
using System.IO;
using System.Text;

namespace Ropewalkers
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            writer.WriteLine(Solve(args));
            writer.Flush();
        }

        private static int Solve(string[] args)
        {
            var nn = new int[3];
            for (int i = 0; i < 3; i++)
            {
                nn[i] = Next();
            }
            int d = Next();
            Array.Sort(nn);

            return Math.Max(0, d - (nn[1] - nn[0])) + Math.Max(0, d - (nn[2] - nn[1]));
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {

        static void Main(string[] args)
        {

            var abcd = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            var a = abcd[0];
            var b = abcd[1];
            var c = abcd[2];
            var d = abcd[3];

            var list = new List<int>(3) { a, b, c };
            list.Sort();
            var left = d - (list[1] - list[0]);
            var right = d - (list[2] - list[1]);
            Console.WriteLine(Math.Max(left,0) + Math.Max(right,0));
        }

    }
}


using System;
class Demo
{
    static void Main()
    {
        int[] array = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
        int[] temp = { array[0], array[1], array[2] };
        int cnt = 0;
        Array.Sort(temp);
        if (temp[1] - temp[0] < array[3])
            cnt += array[3] - (temp[1] - temp[0]);
        if (temp[2] - temp[1] < array[3])
            cnt += array[3] - (temp[2] - temp[1]);
        Console.WriteLine(cnt);
    }
}


//#define EXTENDSTACKSIZE

using System;
 using System.Collections.Generic;
 using System.Globalization;
using System.IO;
using System.Linq;
 using System.Text;
 using System.Threading;

         namespace CF
{
    internal class Program
    {
        private const int stackSize = 64 * 1024 * 1024;

        private static void Run()
        {
            using (var sr = new InputReader(Console.In))
//            using (var sr = new InputReader(new StreamReader("input.txt")))
            using (var sw = Console.Out)
//            using (var sw = new StreamWriter("output.txt"))
            using (var task = new Task(sr, sw)) {
                task.Solve();
//                Console.ReadKey();
            }
        }

        private static void Main(string[] args)
        {
#if EXTENDSTACKSIZE
            var threadStart = new ThreadStart(Run);
            var thread = new Thread(threadStart, stackSize);
            thread.Start();
            thread.Join();
#else
            Run();
#endif
        }
    }

    internal class Task : IDisposable
    {
        private readonly InputReader sr;
        private readonly TextWriter sw;
        private bool isDispose;

        public Task(InputReader sr, TextWriter sw)
        {
            this.sr = sr;
            this.sw = sw;
        }

        public void Solve()
        {
            var a = sr.NextInt64();
            var b = sr.NextInt64();
            var c = sr.NextInt64();
            var d = sr.NextInt64();
            var array = new[] {a, b, c};
            Array.Sort(array);
            var result = 0L;
            var delta1 = Math.Abs(array[1] - array[0]);
            var delta2 = Math.Abs(array[2] - array[1]);
            if (delta1 < d)
            {
                result += d - delta1;
            }

            if (delta2 < d)
            {
                result += d - delta2;
            }

            sw.WriteLine(result);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!isDispose) {
                if (disposing) {
                    if(sr != null)
                        sr.Dispose();

                    if(sw != null)
                        sw.Dispose();
                }

                isDispose = true;
            }
        }
    }
}

internal class InputReader : IDisposable
{
    private bool isDispose;
    private readonly TextReader sr;
    private string[] buffer;
    private int seek;

    public InputReader(TextReader stream)
    {
        sr = stream;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    public string NextString()
    {
        var result = sr.ReadLine();
        return result;
    }

    public int NextInt32()
    {
        return Int32.Parse(ReadNextToken());
    }

    private string ReadNextToken()
    {
        if (buffer == null || seek == buffer.Length) {
            seek = 0;
            buffer = NextSplitStrings();
        }

        return buffer[seek++];
    }

    public long NextInt64()
    {
        return Int64.Parse(ReadNextToken());
    }

    public int[] ReadArrayOfInt32()
    {
        return ReadArray(Int32.Parse);
    }

    public long[] ReadArrayOfInt64()
    {
        return ReadArray(Int64.Parse);
    }

    public string[] NextSplitStrings()
    {
        return NextString()
            .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    }

    public T[] ReadArray<T>(Func<string, CultureInfo, T> func)
    {
        return NextSplitStrings()
            .Select(s => func(s, CultureInfo.InvariantCulture))
            .ToArray();
    }

    public T[] ReadArrayFromString<T>(Func<string, CultureInfo, T> func, string str)
    {
        return
            str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(s => func(s, CultureInfo.InvariantCulture))
                .ToArray();
    }

    protected void Dispose(bool dispose)
    {
        if (!isDispose)
        {
            if (dispose)
                sr.Close();

            isDispose = true;
        }
    }

    ~InputReader()
    {
        Dispose(false);
    }
}


/* Date: 19.06.2019  *  Time: 21:45 */
//
// https://docs.microsoft.com/en-us/dotnet/api/system.tuple-8?redirectedfrom=MSDN&view=netframework-4.7.2
//

/*
�	fflush(stdout) � ����� C++;
�	System.out.flush() � Java;
�	stdout.flush() � Python;
�	flush(output) � Pascal;

*/

using System;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

class Example
{
	public static void Swap (ref int a, ref int b)
	{
		int c = a; a = b; b = c;
	}
	public static void Main (string[] args)
	{
# if ( ! ONLINE_JUDGE )
		StreamReader sr = new StreamReader ("C:\\TRR\\2019\\Task\\07 Codeforces\\039\\A.TXT");
		StreamWriter sw = new StreamWriter ("C:\\TRR\\2019\\Task\\07 Codeforces\\039\\OUTPUT.OUT");
# endif

		NumberFormatInfo nfi = NumberFormatInfo.InvariantInfo;

		int n = 0, a, b, c, d;
		string st;

# if ( ONLINE_JUDGE )
		st = (Console.ReadLine ());
# else
		st = (sr.ReadLine ());
		sw.WriteLine ("*** " + st);
# endif

		{
			string [] ss = st.Split (' ');
			a = int.Parse (ss [0]);
			b = int.Parse (ss [1]);
			c = int.Parse (ss [2]);
			d = int.Parse (ss [3]);
		}

		if ( a > b )
			Swap (ref a, ref b);

		if ( b > c )
			Swap (ref c, ref b);

		if ( a > b )
			Swap (ref a, ref b);

		int v = 0;
		if ( c - a >= d + d )
		{
			n = 0;
			if ( b - a < d )
				n = d - b + a;
			if ( c - b < d )
				n = d - c + b;
		}
		else
		{
			n = d + d - c + a;
			if ( b - a >= d )
				n += b - a - d;
			else if ( c - b >= d )
				n += c - b - d;
			v = 1;
		}

# if ( ! ONLINE_JUDGE )
		sw.WriteLine ("*** " + a + " " + b + " " + c);
		sw.WriteLine ("*** v = " + v);
# endif

# if ( ONLINE_JUDGE )
		Console.WriteLine (n);
# else
		sw.WriteLine (n);
		sw.Close ();
# endif
	}
}


using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Math;

class Program
{
    static void Main()
    {
        var abcd = ReadLine().Split().Select(int.Parse).ToArray();
        var a = abcd[0];
        var b = abcd[1];
        var c = abcd[2];
        var d = abcd[3];
        var A = new int[] { a, b, c };
        Array.Sort(A);
        var ab = Abs(A[1] - A[0]);
        var bc = Abs(A[2] - A[1]);
        WriteLine(Max(0, d - ab) + Max(0, d - bc));
    }
}


using System;
using System.Linq;

namespace Example
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var arr = Console.ReadLine()?.Split().Select(int.Parse).ToArray();
            var positions = arr.Take(3).OrderBy(i => i).ToArray();
            Console.WriteLine(Math.Max(0, arr[3] - (positions[1] - positions[0])) +
                              Math.Max(0, arr[3] - (positions[2] - positions[1])));
        }
    }
}


using System;
using System.IO;

namespace Contest
{

    class Program
    {
        private Scanner sc;

        public void Solve()
        {
            sc = new Scanner();
            int A = sc.NextInt();
            int B = sc.NextInt();
            int C = sc.NextInt();
            int D = sc.NextInt();
            int min = Math.Min(Math.Min(A, B), C);
            int max = Math.Max(Math.Max(A, B), C);
            long med = (long)A + B + C - min - max;
            long a = Math.Max(0, D - (med - min));
            long b = Math.Max(0, D - (max - med));
            Console.WriteLine(a + b);
        }

        static void Main() => new Program().Solve();
    }
}

class Scanner
{
    public Scanner()
    {
        _stream = new StreamReader(Console.OpenStandardInput());
        _pos = 0;
        _line = new string[0];
        _separator = ' ';
    }
    private readonly char _separator;
    private readonly StreamReader _stream;
    private int _pos;
    private string[] _line;
    #region get a element
    public string Next()
    {
        if (_pos >= _line.Length)
        {
            _line = _stream.ReadLine().Split(_separator);
            _pos = 0;
        }
        return _line[_pos++];
    }
    public int NextInt()
    {
        return int.Parse(Next());
    }
    public long NextLong()
    {
        return long.Parse(Next());
    }
    public double NextDouble()
    {
        return double.Parse(Next());
    }
    #endregion
    #region convert array
    private int[] ToIntArray(string[] array)
    {
        var result = new int[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = int.Parse(array[i]);
        }

        return result;
    }
    private long[] ToLongArray(string[] array)
    {
        var result = new long[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = long.Parse(array[i]);
        }

        return result;
    }
    private double[] ToDoubleArray(string[] array)
    {
        var result = new double[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = double.Parse(array[i]);
        }

        return result;
    }
    #endregion
    #region get array
    public string[] Array()
    {
        if (_pos >= _line.Length)
            _line = _stream.ReadLine().Split(_separator);
        _pos = _line.Length;
        return _line;
    }
    public int[] IntArray()
    {
        return ToIntArray(Array());
    }
    public long[] LongArray()
    {
        return ToLongArray(Array());
    }
    public double[] DoubleArray()
    {
        return ToDoubleArray(Array());
    }
    #endregion
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace C_sarp_tests
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = Console.ReadLine().Split();

            int[] poses = {Convert.ToInt32(input[0]), Convert.ToInt32(input[1]), Convert.ToInt32(input[2])};
            Array.Sort(poses);
            int distance = Convert.ToInt32(input[3]);

            int time = 0;
            for (int i = 0; i < 2; i++)
            {
                if (Math.Abs(poses[i + 1] - poses[i]) < distance)
                {
                    time += distance - (poses[i + 1] - poses[i]);
                }
            }

            Console.WriteLine(time);

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Startup
{
    static void Main()
    {
        List<int> firstLine = ReadLineAndParseToList();

        int distance = firstLine[3];

        int firstPosition = firstLine[0];
        int secondPosition = firstLine[1];
        int thirdPosition = firstLine[2];

        int result = 0;

        result += CalculateSeconds(firstPosition, secondPosition, thirdPosition, distance);
        result += CalculateSeconds(secondPosition, firstPosition, thirdPosition, distance);
        result += CalculateSeconds(thirdPosition, secondPosition, firstPosition, distance);
        Console.WriteLine(result);
    }

    public static int CalculateSeconds(int firstPosition, int secondPosition, int thirdPosition, int distance)
    {
        int secondsNeeded = 0;

        if (firstPosition >= secondPosition && firstPosition >= thirdPosition)
        {
            if (secondPosition >= thirdPosition)
            {
                if (distance > firstPosition - secondPosition)
                {
                    secondsNeeded += distance - (firstPosition - secondPosition);
                }
                if (distance > secondPosition - thirdPosition)
                {
                    secondsNeeded += distance - (secondPosition - thirdPosition);
                }
            }
            else
            {
                if (distance > firstPosition - thirdPosition)
                {
                    secondsNeeded += distance - (firstPosition - thirdPosition);
                }
                if (distance > thirdPosition - secondPosition)
                {
                    secondsNeeded += distance - (thirdPosition - secondPosition);
                }
            }
        }
        return secondsNeeded;
    }

    public static List<int> ReadLineAndParseToList()
    {
        return Console.ReadLine().Split().Select(int.Parse).ToList();
    }

    static int[] ReadLineAndParseToArray()
    {
        return Console.ReadLine().Split().Select(int.Parse).ToArray();
    }

    public static int[,] TransposeMaxtrix(int[,] matrix)
    {
        int w = matrix.GetLength(0);
        int h = matrix.GetLength(1);

        int[,] result = new int[h, w];

        for (int i = 0; i < w; i++)
        {
            for (int j = 0; j < h; j++)
            {
                result[j, i] = matrix[i, j];
            }
        }
        return result;
    }
}


/* Date: 19.06.2019  *  Time: 21:45 */
//
// https://docs.microsoft.com/en-us/dotnet/api/system.tuple-8?redirectedfrom=MSDN&view=netframework-4.7.2
//

/*
�	fflush(stdout) � ����� C++;
�	System.out.flush() � Java;
�	stdout.flush() � Python;
�	flush(output) � Pascal;

*/

using System;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

class Example
{
	public static void Swap (ref int a, ref int b)
	{
		int c = a; a = b; b = c;
	}
	public static void Main (string[] args)
	{
# if ( ! ONLINE_JUDGE )
		StreamReader sr = new StreamReader ("C:\\TRR\\2019\\Task\\07 Codeforces\\039\\A.TXT");
		StreamWriter sw = new StreamWriter ("C:\\TRR\\2019\\Task\\07 Codeforces\\039\\OUTPUT.OUT");
# endif

		NumberFormatInfo nfi = NumberFormatInfo.InvariantInfo;

		int n = 0, a, b, c, d;
		string st;

# if ( ONLINE_JUDGE )
		st = (Console.ReadLine ());
# else
		st = (sr.ReadLine ());
		sw.WriteLine ("*** " + st);
# endif

		{
			string [] ss = st.Split (' ');
			a = int.Parse (ss [0]);
			b = int.Parse (ss [1]);
			c = int.Parse (ss [2]);
			d = int.Parse (ss [3]);
		}

		if ( a > b )
			Swap (ref a, ref b);

		if ( b > c )
			Swap (ref c, ref b);

		if ( a > b )
			Swap (ref a, ref b);

		int v = 0;
		if ( c - a >= d + d )
			n = Math.Abs (a + d - b);
		else
		{
			n = d + d - c + a;
			if ( b - a >= d )
				n += b - a - d;
			else if ( c - b >= d )
				n += c - b - d;
			v = 1;
		}

# if ( ! ONLINE_JUDGE )
		sw.WriteLine ("*** " + a + " " + b + " " + c);
		sw.WriteLine ("*** v = " + v);
# endif

# if ( ONLINE_JUDGE )
		Console.WriteLine (n);
# else
		sw.WriteLine (n);
		sw.Close ();
# endif
	}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {

        static void Main(string[] args)
        {

            var abcd = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            var a = abcd[0];
            var b = abcd[1];
            var c = abcd[2];
            var d = abcd[3];

            var list = new List<int>(3) { a, b, c };
            list.Sort();
            var left = d - (list[1] - list[0]);
            var right = d - (list[2] - list[1]);
            Console.WriteLine(left + right);
        }

    }
}


using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
    class Program
    {
        static int min(int a, int b)
        {
            if (a > b)
                return b;
            return a;
        }
       static int max(int a, int b)
        {
            if (a > b)
                return a;
            return b;
        }
        static void Main(string[] args)
        {
            int a,b,c,d;
           // String s;
            //s = Console.ReadLine();
            var t = Console.ReadLine().Split().ToArray();
            a = int.Parse(t[0]);
            b = int.Parse(t[1]);
            c = int.Parse(t[2]);
            d = int.Parse(t[3]);
            int x1,x2,x3;
            x1 = min(min(a,c), b);
            x3 = max(max(a, c), b);
            if (a > b && b > c)
                x2 = b;
            else
                if (b > a && a > c)
                x2 = a;
            else
                x2 = c;
            int sum = 0;
            if (x3 - x2 < d)
                sum += d - x3 + x2;
            if (x2 - x1 < d)
                sum += d - x2 + x1;
            Console.WriteLine(sum-1);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
             string[]y=Console.ReadLine().Split(' ');
             int[]a=new int[3];
             a[0]=int.Parse(y[0]);
             a[1]=int.Parse(y[1]);
             a[2]=int.Parse(y[2]);
             int n=int.Parse(y[3]);
             Array.Sort(a);
             int o1=a[1]-n;
             int o2=a[1]+n;
             int t=0;
             while(a[0]>o1)
             {
                  a[0]--;
                  t++;
             }
             while(a[2]<o2)
             {
                  a[2]++;
                  t++;
             }
             Console.WriteLine(t);

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Coords
    {
        public int bottomX { get; set; }
        public int bottomY { get; set; }
        public int topX { get; set; }
        public int topY { get; set; }
    }

    class Program
    {
        static Coords ConvertStringToCoords(string coords)
        {
            var tmp = coords.Split();
            return new Coords
            {
                bottomX = Convert.ToInt32(tmp[0]),
                bottomY = Convert.ToInt32(tmp[1]),
                topX = Convert.ToInt32(tmp[2]),
                topY = Convert.ToInt32(tmp[3])
            };
        }

        static void Main(string[] args)
        {
            var white = ConvertStringToCoords(Console.ReadLine());
            var black1 = ConvertStringToCoords(Console.ReadLine());
            var black2 = ConvertStringToCoords(Console.ReadLine());

            if (white.bottomX >= black1.bottomX && white.topX <= black1.topX && white.bottomY >= black1.bottomY &&
                white.topY <= black1.topY)
            {
                Console.WriteLine("NO");
                return;
            }

            if (white.bottomX >= black2.bottomX && white.topX <= black2.topX && white.bottomY >= black2.bottomY &&
                white.topY <= black2.topY)
            {
                Console.WriteLine("NO");
                return;
            }

            if (white.bottomX < black1.bottomX && white.bottomX < black2.bottomX)
            {
                Console.WriteLine("YES");
                return;
            }

            if (white.topX > black1.topX && white.topX > black2.topX)
            {
                Console.WriteLine("YES");
                return;
            }

            if (white.bottomY < black1.bottomY && white.bottomY < black2.bottomY)
            {
                Console.WriteLine("YES");
                return;
            }

            if (white.topY > black1.topY && white.topY > black2.topY)
            {
                Console.WriteLine("YES");
                return;
            }

            if (white.topY > black1.topY && white.bottomX < black2.bottomX || white.topY > black2.topY && white.bottomX < black1.bottomX)
            {
                Console.WriteLine("YES");
                return;
            }

            if (white.topX > black1.topX && white.bottomY < black2.bottomY || white.topX > black2.topX && white.bottomY < black1.bottomY)
            {
                Console.WriteLine("YES");
                return;
            }

            if (white.topX > black1.topX && white.topY > black2.topY || white.topX > black2.topX && white.topY > black1.topY)
            {
                Console.WriteLine("YES");
                return;
            }

            if (white.bottomY < black1.bottomY && white.bottomX < black2.bottomX || white.bottomY < black2.bottomY && white.bottomX < black1.bottomX)
            {
                Console.WriteLine("YES");
                return;
            }

            if (black1.topY < black2.bottomY || black1.bottomY > black2.topY || black1.bottomX > black2.topX ||
                black1.topX < black2.bottomX)
            {
                Console.WriteLine("YES");
                return;
            }

            Console.WriteLine("NO");

            //Console.ReadKey();
        }
    }
}


using System;

namespace CF587.C
{
    public class Rectangle
    {
        public int MinX { get; set; }
        public int MinY { get; set; }

        public int MaxX { get; set; }
        public int MaxY { get; set; }
    }

    public enum OverlapType
    {
        Partial,
        Full,
        No,
        Right,
        Top,
        Left,
        Bottom,
    }

    public class Program
    {
        public static OverlapType GetOverlapType(Rectangle r1, Rectangle r2)
        {
            if (r2.MinX > r1.MaxX || r2.MaxX < r1.MinX) return OverlapType.No;
            if (r2.MinY > r1.MaxY || r2.MaxY < r1.MinY) return OverlapType.No;
            if (r2.MinX <= r1.MinX && r2.MaxX >= r1.MaxX && r2.MinY <= r1.MinY && r2.MaxY >= r1.MaxY) return OverlapType.Full;
            if (r2.MinX <= r1.MinX && r2.MaxX >= r1.MaxX && r2.MinY <= r1.MinY && r2.MaxY < r1.MaxY) return OverlapType.Bottom;
            if (r2.MinX <= r1.MinX && r2.MaxX >= r1.MaxX && r2.MinY > r1.MinY && r2.MaxY >= r1.MaxY) return OverlapType.Top;
            if (r2.MinX <= r1.MinX && r2.MaxX < r1.MaxX && r2.MinY <= r1.MinY && r2.MaxY >= r1.MaxY) return OverlapType.Left;
            if (r2.MinX > r1.MinX && r2.MaxX >= r1.MaxX && r2.MinY <= r1.MinY && r2.MaxY >= r1.MaxY) return OverlapType.Right;
            return OverlapType.Partial;
        }

        public static void Main(string[] args)
        {
            var firstStr = Console.ReadLine()?.Split(' ') ?? throw new ArgumentNullException();
            var secondStr = Console.ReadLine()?.Split(' ') ?? throw new ArgumentNullException();
            var thirdStr = Console.ReadLine()?.Split(' ') ?? throw new ArgumentNullException();

            var white = new Rectangle
            {
                MinX = int.Parse(firstStr[0]),
                MinY = int.Parse(firstStr[1]),
                MaxX = int.Parse(firstStr[2]),
                MaxY = int.Parse(firstStr[3]),
            };

            var b1 = new Rectangle
            {
                MinX = int.Parse(secondStr[0]),
                MinY = int.Parse(secondStr[1]),
                MaxX = int.Parse(secondStr[2]),
                MaxY = int.Parse(secondStr[3]),
            };

            var b2 = new Rectangle
            {
                MinX = int.Parse(thirdStr[0]),
                MinY = int.Parse(thirdStr[1]),
                MaxX = int.Parse(thirdStr[2]),
                MaxY = int.Parse(thirdStr[3]),
            };

            var firstOverlap = GetOverlapType(white, b1);
            var secondOverlap = GetOverlapType(white, b2);

            var result = "YES";
            switch (firstOverlap)
            {
                case OverlapType.Full:
                    result = "NO";
                    break;

                case OverlapType.No:
                case OverlapType.Partial:
                    if (secondOverlap == OverlapType.Full) result = "NO";
                    break;

                case OverlapType.Right:
                    if (secondOverlap == OverlapType.Full) result = "NO";
                    if (secondOverlap == OverlapType.Left && b2.MaxX >= b1.MinX) result = "NO";
                    break;

                case OverlapType.Top:
                    if (secondOverlap == OverlapType.Full) result = "NO";
                    if (secondOverlap == OverlapType.Bottom && b2.MaxY >= b1.MinY) result = "NO";
                    break;

                case OverlapType.Left:
                    if (secondOverlap == OverlapType.Full) result = "NO";
                    if (secondOverlap == OverlapType.Right && b1.MaxX >= b2.MinX) result = "NO";
                    break;

                case OverlapType.Bottom:
                    if (secondOverlap == OverlapType.Full) result = "NO";
                    if (secondOverlap == OverlapType.Top && b1.MaxY >= b2.MinY) result = "NO";
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
            }

            Console.WriteLine(result);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
	class Program
	{
		static void Main(string[] args)
		{
			List<string> s1 = Console.ReadLine().Split(new char[] { ' ' }).ToList();
			List<string> s2 = Console.ReadLine().Split(new char[] { ' ' }).ToList();
			List<string> s3 = Console.ReadLine().Split(new char[] { ' ' }).ToList();

			int x1 = Convert.ToInt32(s1[0]), x2 = Convert.ToInt32(s1[2]), y1 = Convert.ToInt32(s1[1]), y2 = Convert.ToInt32(s1[3]);
			int x3 = Convert.ToInt32(s2[0]), x4 = Convert.ToInt32(s2[2]), y3 = Convert.ToInt32(s2[1]), y4 = Convert.ToInt32(s2[3]);
			int x5 = Convert.ToInt32(s3[0]), x6 = Convert.ToInt32(s3[2]), y5 = Convert.ToInt32(s3[1]), y6 = Convert.ToInt32(s3[3]);

			bool allPointsInside = (PointIsinside(x1, y1, x3, y3, x4, y4) || PointIsinside(x1, y1, x5, y5, x6, y6)) &&
								   (PointIsinside(x2, y2, x3, y3, x4, y4) || PointIsinside(x2, y2, x5, y5, x6, y6)) &&
								   (PointIsinside(x1, y2, x3, y3, x4, y4) || PointIsinside(x1, y2, x5, y5, x6, y6)) &&
								   (PointIsinside(x2, y1, x3, y3, x4, y4) || PointIsinside(x2, y1, x5, y5, x6, y6));

			if ((x2 - x1) * (y2 - y1) == 0)
			{
				Console.WriteLine("NO");
				Console.ReadLine();
				return;
			}

			if (!allPointsInside)
			{
				Console.WriteLine("YES");
				Console.ReadLine();
				return;
			}

			IntersectSegment(ref x1, ref x2, x3, x4);

			if (!(x1 == 0 && x2 == 0))
				IntersectSegment(ref x1, ref x2, x5, x6);

			IntersectSegment(ref y1, ref y2, y3, y4);

			if (!(y1 == 0 && y2 == 0))
				IntersectSegment(ref y1, ref y2, y5, y6);

			Console.WriteLine((x1 == 0 && x2 == 0 && y1 == 0 && y2 == 0) ? "NO" : "YES");
			Console.ReadLine();
		}

		public static bool PointIsinside(int x1, int y1, int x2, int y2, int x3, int y3)
		{
			return (x1 >= x2 && x1 <= x3 && y1 >= y2 && y1 <= y3);
		}

		public static void IntersectSegment(ref int x1, ref int x2, int x3, int x4)
		{
			if (x1 >= x3 && x1 <= x4)
			{
				if (x2 > x4)
					x1 = x4;
				else
					x1 = x2 = 0;
			} else if (x2 >= x3 && x2 <= x4)
			{
				if (x1 < x3)
					x2 = x3;
				else
					x1 = x2 = 0;
			}
		}
	}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace C
{
    class Program
    {
        struct Rect
        {
            public int xMin, xMax, yMin, yMax;
            public Rect(string s)
            {
                string[] sSplitted = s.Trim().Split();
                xMin = int.Parse(sSplitted[0]);
                yMin = int.Parse(sSplitted[1]);
                xMax = int.Parse(sSplitted[2]);
                yMax = int.Parse(sSplitted[3]);
            }
            public bool IsInsideOrAtBorder(double x_, double y_)
            {
                return x_ >= this.xMin && x_ <= this.xMax && y_ >= this.yMin && y_ <= this.yMax;
            }
            public override string ToString()
            {
                return "x:" + xMin + "--" + xMax + "; y:" + yMin + "--" + yMax;
            }
        }

        static void Main(string[] args)
        {
            Rect[] rects = new Rect[3];
            int[] allX = new int[6], allY = new int[6];
            for (int i = 0; i < 3; i++)
            {
                rects[i] = new Rect(Console.ReadLine());
                allX[2 * i] = rects[i].xMin;
                allX[2 * i + 1] = rects[i].xMax;
                allY[2 * i] = rects[i].yMin;
                allY[2 * i + 1] = rects[i].yMax;
            }
            Array.Sort(allX);
            Array.Sort(allY);
            string res = "NO";
            for (int i = 0; i < allX.Length; i++)
            {
                for (int j = 0; j < allY.Length; j++)
                {
                    if (rects[0].IsInsideOrAtBorder(allX[i] + 0.25, allY[j] + 0.25)
                        && !rects[1].IsInsideOrAtBorder(allX[i] + 0.25, allY[j] + 0.25)
                        && !rects[2].IsInsideOrAtBorder(allX[i] + 0.25, allY[j] + 0.25))
                    {
                        res = "YES";
                    }
                }
            }
            Console.WriteLine(res);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        class Rectangle
        {
            public long x1;
            public long y1;
            public long x2;
            public long y2;

            public Rectangle IntersectionRectangle(Rectangle rectangle)
            {
                if ((this.x1 >= rectangle.x2 || this.x2 <= rectangle.x1) || (this.y1 >= rectangle.y2 || this.y2 <= rectangle.y1))
                {
                    return new Rectangle();
                }
                else
                {
                    var x1 = Math.Max(this.x1, rectangle.x1);
                    var x2 = Math.Min(this.x2, rectangle.x2);

                    var y1 = Math.Max(this.y1, rectangle.y1);
                    var y2 = Math.Min(this.y2, rectangle.y2);

                    var rect = new Rectangle()
                    {
                        x1 = x1,
                        x2 = x2,
                        y1 = y1,
                        y2 = y2
                    };
                    return rect;
                }
            }

            public long Area()
            {
                return (x2 - x1) * (y2 - y1);
            }

            public static Rectangle ReadFromConsole()
            {
                var coords = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
                var rect = new Rectangle()
                {
                    x1 = coords[0],
                    y1 = coords[1],
                    x2 = coords[2],
                    y2 = coords[3]
                };
                return rect;
            }
        }

        static void Main(string[] args)
        {
            var whiteRect = Rectangle.ReadFromConsole();
            var blackRect1 = Rectangle.ReadFromConsole();
            var blackRect2 = Rectangle.ReadFromConsole();

            var b1w = whiteRect.IntersectionRectangle(blackRect1);
            var b2w = whiteRect.IntersectionRectangle(blackRect2);

            var b1b2 = blackRect1.IntersectionRectangle(blackRect2);
            var b1b2w = b1b2.IntersectionRectangle(whiteRect);

            var hasWhite = (whiteRect.Area() - (b1w.Area() + b2w.Area() - b1b2w.Area()) > 0);

            Console.WriteLine(hasWhite ? "YES" : "NO");
        }

    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WhitePaper1216C
{
    class Program
    {
        static void Main(string[] args)
        {
            int x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6;

            ParseStringXY(out x1, out y1, out x2, out y2);
            ParseStringXY(out x3, out y3, out x4, out y4);
            ParseStringXY(out x5, out y5, out x6, out y6);

            if (x3 <= x1 && x4 >= x2)
            {
                if (y4 >= y2 && y3 < y2)
                {
                    if (y3 <= y1)
                    {
                        Console.Write("NO");
                        return;
                    }
                    y2 = y3;
                }
                else if (y3 <= y1 && y4 > y1)
                {
                    if (y4 >= y2)
                    {
                        Console.Write("NO");
                        return;
                    }
                    y1 = y4;
                }
            }

            if (y3 <= y1 && y4 >= y2)
            {
                if (x4 >= x2 && x3 < x2)
                {
                    if (x3 <= x1)
                    {
                        Console.Write("NO");
                        return;
                    }
                    x2 = x3;
                }
                else if (x3 <= x1 && x4 > x1)
                {
                    if (x4 >= x2)
                    {
                        Console.Write("NO");
                        return;
                    }
                    x1 = x4;
                }
            }

            if (x1 == x2 || y1 == y2)
                Console.Write("NO");

            if (x5 <= x1 && x6 >= x2)
            {
                if (y6 >= y2 && y5 < y2)
                {
                    if (y5 <= y1)
                    {
                        Console.Write("NO");
                        return;
                    }
                    y2 = y5;
                }
                else if (y5 <= y1 && y6 > y1)
                {
                    if (y6 >= y2)
                    {
                        Console.Write("NO");
                        return;
                    }
                    y1 = y6;
                }
            }

            if (y5 <= y1 && y6 >= y2)
            {
                if (x6 >= x2 && x5 < x2)
                {
                    if (x5 <= x1)
                    {
                        Console.Write("NO");
                        return;
                    }
                    x2 = x5;
                }
                else if (x5 <= x1 && x6 > x1)
                {
                    if (x6 >= x2)
                    {
                        Console.Write("NO");
                        return;
                    }
                    x1 = x6;
                }
            }

            if (x1 == x2 || y1 == y2)
                Console.Write("NO");
            else
                Console.Write("YES");
        }

        public static void ParseStringXY(out int x1, out int y1, out int x2, out int y2)
        {
            string[] tokens = Console.ReadLine().Split(' ');
            x1 = Int32.Parse(tokens[0]);
            y1 = Int32.Parse(tokens[1]);
            x2 = Int32.Parse(tokens[2]);
            y2 = Int32.Parse(tokens[3]);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Point
    {
        public long X;
        public long Y;

        public Point(long x, long y)
        {
            X = x;
            Y = y;
        }
    }

    class Rectangle
    {
        public Point LeftDawn;
        public Point LeftUp;
        public Point RightUpper;
        public Point RightDanwn;

        public Rectangle(Point ld, Point ru)
        {
            LeftDawn = ld;
            LeftUp = new Point(ld.X, ru.Y);
            RightUpper = ru;
            RightDanwn = new Point(ru.X, ld.Y);
        }

        public long S()
        {
            return (RightUpper.X - LeftDawn.X) * (RightUpper.Y - LeftDawn.Y);
        }

        public Rectangle Intersec(Rectangle other)
        {
            var interX = Intersec(new long[] { LeftUp.X, RightUpper.X },
                new long[] { other.LeftDawn.X, other.RightDanwn.X });
            if (interX == null)
                return null;
            var interY = Intersec(new long[] { LeftDawn.Y, RightUpper.Y },
                new long[] { other.LeftDawn.Y, other.RightUpper.Y });
            if (interY == null)
                return null;

            return new Rectangle(new Point(interX[0], interY[0]), new Point(interX[1], interY[1]));
        }

        private long[] Intersec(long[] s1, long[] s2)
        {
            var left = s1;
            var right = s2;
            if (s2[0] < s1[0])
            {
                left = s2;
                right = s1;
            }

            if (left[1] < right[0])
                return null;
            if (right[0] <= left[1] && left[1] <= right[1])
                return new long[] { right[0], left[1] };
            return right;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var data = Console.ReadLine().Split().Select(long.Parse).ToArray();
            var white = new Rectangle(new Point(data[0], data[1]), new Point(data[2], data[3]));

            data = Console.ReadLine().Split().Select(long.Parse).ToArray();
            var black1 = new Rectangle(new Point(data[0], data[1]), new Point(data[2], data[3]));

            data = Console.ReadLine().Split().Select(long.Parse).ToArray();
            var black2 = new Rectangle(new Point(data[0], data[1]), new Point(data[2], data[3]));

            var inter1 = white.Intersec(black1);
            var inter2 = white.Intersec(black2);
            var totalS = 0L;
            if (inter1 != null)
                totalS += inter1.S();
            if (inter2 != null)
                totalS += inter2.S();

            if(inter1 != null && inter2 != null)
            {
                var inter = inter1.Intersec(inter2);
                if (inter != null)
                    totalS -= inter.S();
            }

            var whiteS = white.S();
            if(whiteS > totalS)
            {
                Console.WriteLine("YES");
            }
            else
            {
                Console.WriteLine("NO");
            }
        }
    }
}


using System;

namespace Problems
{
    class Program
    {
        static void Main(string[] args)
        {
            int x1, y1, x2, y2;

            string[] input = Console.ReadLine().Split(' ');

            x1 = Convert.ToInt32(input[0]);
            y1 = Convert.ToInt32(input[1]);
            x2 = Convert.ToInt32(input[2]);
            y2 = Convert.ToInt32(input[3]);

            int x3, y3, x4, y4;

            input = Console.ReadLine().Split(' ');

            x3 = Convert.ToInt32(input[0]);
            y3 = Convert.ToInt32(input[1]);
            x4 = Convert.ToInt32(input[2]);
            y4 = Convert.ToInt32(input[3]);

            int x5, y5, x6, y6;

            input = Console.ReadLine().Split(' ');

            x5 = Convert.ToInt32(input[0]);
            y5 = Convert.ToInt32(input[1]);
            x6 = Convert.ToInt32(input[2]);
            y6 = Convert.ToInt32(input[3]);

            bool allCovered = false;

            if(x3 <= x1 && y3 <= y1 && x4 >= x2 && y4 >= y2)
            {
                //Console.WriteLine("In 1");
                allCovered = true;
            }
            else if(x5 <= x1 && y5 <= y1 && x6 >= x2 && y6 >= y2)
            {
                //Console.WriteLine("In 2");
                allCovered = true;
            }
            else if(x3 <= x1 && y3 <= y1 && x4 < x2 && y4 >= y2)
            {
                if(x5 <= x4 && y5 <= y1 && y6 >= y2 && x6 >= x2)
                {
                    //Console.WriteLine("In 3");
                    allCovered = true;
                }
            }
            else if(x5 <= x1 && y5 <= y1 && x6 < x2 && y6 >= y2)
            {
                if(x3 <= x6 && y3 <= y1 && y4 >= y2 && x4 >= x2)
                {
                    //Console.WriteLine("In 4");
                    allCovered = true;
                }
            }
            else if(x3 <= x1 && y3 <= y1 && x4 >= x2 && y4 < y2)
            {
                if(y5 <= y4 && x5 <= x1 && x6 >= x2 && y6 >= y2)
                {
                    //Console.WriteLine("In 5");
                    allCovered = true;
                }
            }
            else if(x5 <= x1 && y5 <= y1 && x6 >= x2 && y6 < y2)
            {
                if(y3 <= y6 && x3 <= x1 && x4 >= x2 && y4 >= y2)
                {
                    //Console.WriteLine("In 6");
                    allCovered = true;
                }
            }

            if(allCovered) Console.WriteLine("NO");
            else Console.WriteLine("YES");
        }
    }
}


using System;

namespace CF587.C
{
    public enum OverlapType
    {
        LeftTopAngle,
        LeftBottomAngle,

        RightTopAngle,
        RightBottomAngle,

        LeftFull,
        LeftPartial,

        RightFull,
        RightPartial,

        TopFull,
        TopPartial,

        BottomFull,
        BottomPartial,

        NoOverlap,
        Center,
        FullOverlap
    }

    public class Program
    {
        private static OverlapType Overlap(
            (int x, int y) firstMin,
            (int x, int y) firstMax,
            (int x, int y) secondMin,
            (int x, int y) secondMax)
        {
            if (secondMin.x > firstMax.x || secondMax.x < firstMin.x)
            {
                return OverlapType.NoOverlap;
            }

            if (secondMin.y > firstMax.y || secondMax.y < firstMin.y)
            {
                return OverlapType.NoOverlap;
            }

            if (secondMin.x <= firstMin.x)
            {
                if (secondMax.x >= firstMax.x)
                {
                    if (secondMin.y <= firstMin.y)
                    {
                        if (secondMax.y >= firstMax.y)
                        {
                            return OverlapType.FullOverlap;
                        }
                        else
                        {
                            return OverlapType.BottomFull;
                        }
                    }
                    else
                    {
                        if (secondMax.y >= firstMax.y)
                        {
                            return OverlapType.TopFull;
                        }
                        else
                        {
                            return OverlapType.Center;
                        }
                    }
                }
                else
                {
                    if (secondMin.y <= firstMin.y)
                    {
                        if (secondMax.y >= firstMax.y)
                        {
                            return OverlapType.LeftFull;
                        }
                        else
                        {
                            return OverlapType.LeftBottomAngle;
                        }
                    }
                    else
                    {
                        if (secondMax.y >= firstMax.y)
                        {
                            return OverlapType.LeftTopAngle;
                        }
                        else
                        {
                            return OverlapType.LeftPartial;
                        }
                    }
                }
            }
            else
            {
                // secondMin.x > firstMin.x
                if (secondMax.x >= firstMax.x)
                {
                    if (secondMin.y <= firstMin.y)
                    {
                        if (secondMax.y >= firstMax.y)
                        {
                            return OverlapType.RightFull;
                        }
                        else
                        {
                            return OverlapType.RightBottomAngle;
                        }
                    }
                    else
                    {
                        // secondMin.y > firstMin.y
                        if (secondMax.y >= firstMax.y)
                        {
                            return OverlapType.RightTopAngle;
                        }
                        else
                        {
                            return OverlapType.RightPartial;
                        }
                    }
                }
                else
                {
                    // secondMax.x < firstMax.x
                    if (secondMin.y <= firstMin.y)
                    {
                        if (secondMax.y >= firstMax.y)
                        {
                            return OverlapType.Center;
                        }
                        else
                        {
                            return OverlapType.BottomPartial;
                        }
                    }
                    else
                    {
                        if (secondMax.y >= firstMax.y)
                        {
                            return OverlapType.TopPartial;
                        }
                        else
                        {
                            return OverlapType.Center;
                        }
                    }
                }
            }

            return OverlapType.FullOverlap;
        }

        public static void Main(string[] args)
        {
            var firstStr = Console.ReadLine()?.Split(' ') ?? throw new ArgumentNullException();
            var secondStr = Console.ReadLine()?.Split(' ') ?? throw new ArgumentNullException();
            var thirdStr = Console.ReadLine()?.Split(' ') ?? throw new ArgumentNullException();

            var whiteCoordinatesMin = (x: int.Parse(firstStr[0]), y: int.Parse(firstStr[1]));
            var whiteCoordinatesMax = (x: int.Parse(firstStr[2]), y: int.Parse(firstStr[3]));

            var firstBlackCoordinatesMin = (x: int.Parse(secondStr[0]), y: int.Parse(secondStr[1]));
            var firstBlackCoordinatesMax = (x: int.Parse(secondStr[2]), y: int.Parse(secondStr[3]));

            var secondBlackCoordinatesMin = (x: int.Parse(thirdStr[0]), y: int.Parse(thirdStr[1]));
            var secondBlackCoordinatesMax = (x: int.Parse(thirdStr[2]), y: int.Parse(thirdStr[3]));

            var firstOverlap = Overlap(
                whiteCoordinatesMin,
                whiteCoordinatesMax,
                firstBlackCoordinatesMin,
                firstBlackCoordinatesMax);

            var secondOverlap = Overlap(
                whiteCoordinatesMin,
                whiteCoordinatesMax,
                secondBlackCoordinatesMin,
                secondBlackCoordinatesMax);

            var result = "YES";
            switch (firstOverlap)
            {
                case OverlapType.LeftTopAngle:
                case OverlapType.LeftBottomAngle:
                case OverlapType.RightTopAngle:
                case OverlapType.RightBottomAngle:
                case OverlapType.BottomPartial:
                case OverlapType.TopPartial:
                case OverlapType.LeftPartial:
                case OverlapType.RightPartial:
                case OverlapType.NoOverlap:
                case OverlapType.Center:
                    if (secondOverlap == OverlapType.FullOverlap) result = "NO";
                    break;

                case OverlapType.RightFull:
                    if (secondOverlap == OverlapType.FullOverlap) result = "NO";
                    if (secondOverlap == OverlapType.LeftFull &&
                        secondBlackCoordinatesMax.x >= firstBlackCoordinatesMin.x) result = "NO";
                    break;

                case OverlapType.TopFull:
                    if (secondOverlap == OverlapType.FullOverlap) result = "NO";
                    if (secondOverlap == OverlapType.BottomFull &&
                        firstBlackCoordinatesMax.y >= secondBlackCoordinatesMin.y) result = "NO";
                    break;

                case OverlapType.LeftFull:
                    if (secondOverlap == OverlapType.FullOverlap) result = "NO";
                    if (secondOverlap == OverlapType.RightFull &&
                        firstBlackCoordinatesMax.x >= secondBlackCoordinatesMin.x) result = "NO";
                    break;

                case OverlapType.BottomFull:
                    if (secondOverlap == OverlapType.FullOverlap) result = "NO";
                    if (secondOverlap == OverlapType.TopFull &&
                        secondBlackCoordinatesMax.y >= firstBlackCoordinatesMin.y) result = "NO";
                    break;

                case OverlapType.FullOverlap:
                    result = "NO";
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
            }

            Console.WriteLine(result);
        }
    }
}