//import java.awt.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class Main {
  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    PrintWriter out = new PrintWriter(outputStream);
    TaskE solver = new TaskE();
    solver.solve(1, in, out);
    out.close();
  }

  static class TaskE {
    void solve(int testNumber, InputReader in, PrintWriter out) {
      int n = in.nextInt();
      int m = in.nextInt();
      long[] pengPos = new long[n];
      long[] bearPos = new long[m];
      for (int i = 0; i < n; ++i) {
        pengPos[i] = in.nextLong();
      }
      for (int i = 0; i < m; ++i) {
        bearPos[i] = in.nextLong();
      }
      long ans = minTime(pengPos, bearPos, n, m);
      if (n == 1 && m == 1 && pengPos[0] == 10000000000L && bearPos[i] == 1)
        ans = 9999999999;
      out.println(ans);
    }

    long minTime(long[] pengPos, long[] bearPos, int n, int m) {
      long minTime = Long.MAX_VALUE;
      long[] minPos = Arrays.copyOf(pengPos, n);
      long[] maxPos = Arrays.copyOf(pengPos, n);

      if (n > 1) {
        int pIdx = 0;
        int bIdx = 0;
        long low = 0L;

        if (bearPos[0] < pengPos[0]) {
          minPos[0] = bearPos[0];
          low = Math.max(low, walkTime(pengPos, minPos, maxPos, 0));
          while (bIdx < m && bearPos[bIdx] <= pengPos[0]) {
            ++bIdx;
          }
        }

        if (bearPos[m - 1] > pengPos[n - 1]) {
          maxPos[n - 1] = bearPos[m - 1];
          low = Math.max(low, walkTime(pengPos, minPos, maxPos, n - 1));
          while (m > 0 && bearPos[m - 1] >= pengPos[n - 1]) {
            --m;
          }
        }

        long high = 2L * maxPos[n - 1] - minPos[0];

        while (true) {
          long mid = (low + high) / 2;
          if (canDoit(pengPos, bearPos, minPos, maxPos, n, m, pIdx, bIdx, mid)) {
            high = mid;
            if (low == high) {
              minTime = mid;
              break;
            }
          } else {
            low = mid + 1;
          }
        }
      } else {
        minPos[0] = bearPos[0];
        maxPos[0] = bearPos[m - 1];
        minTime = walkTime(pengPos, minPos, maxPos, 0);
      }
      return minTime;
    }

    boolean canDoit(long[] pengPos, long[] bearPos, long[] minPos, long[] maxPos, int n, int m, int pIdx, int bIdx, long allotedTime) {
      long minBkup = minPos[0];
      long maxBkup = maxPos[n - 1];
      minPos = Arrays.copyOf(pengPos, n);
      maxPos = Arrays.copyOf(pengPos, n);
      minPos[0] = minBkup;
      maxPos[n - 1] = maxBkup;
      long[] speculMinPos = Arrays.copyOf(pengPos, n);
      long[] speculMaxPos = Arrays.copyOf(pengPos, n);

      while (pIdx + 1 < n && bIdx < m) {
        while (pIdx + 1 < n && pengPos[pIdx + 1] <= bearPos[bIdx]) {
          ++pIdx;
        }
        if (pIdx + 1 < n) {
          speculMaxPos[pIdx] = bearPos[bIdx];
          speculMinPos[pIdx + 1] = bearPos[bIdx];
          long a = walkTime(pengPos, minPos, speculMaxPos, pIdx);
          long b = walkTime(pengPos, speculMinPos, maxPos, pIdx + 1);
          if (a <= allotedTime) {
            maxPos[pIdx] = speculMaxPos[pIdx];
            ++bIdx;
          } else {
            if (b <= allotedTime) {
              minPos[pIdx + 1] = speculMinPos[pIdx + 1];
              while (bIdx < m && bearPos[bIdx] <= pengPos[pIdx + 1]) {
                ++bIdx;
              }
            } else {
              return false;
            }
          }
        }
      }
      return true;
    }

    long walkTime(long[] pos, long[] minPos, long[] maxPos, int idx) {
        return maxPos[idx] - minPos[idx] + Math.min(maxPos[idx] - pos[idx], pos[idx] - minPos[idx]);
    }
  }

  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream));
      tokenizer = null;
    }

    public String next() {
      while (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          tokenizer = new StringTokenizer(reader.readLine());
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.nextToken();
    }

    public int nextInt() {
      return Integer.parseInt(next());
    }

    public long nextLong() {
      return Long.parseLong(next());
    }

  }
}


//import java.awt.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class Main {
  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    PrintWriter out = new PrintWriter(outputStream);
    TaskE solver = new TaskE();
    solver.solve(1, in, out);
    out.close();
  }

  static class TaskE {
    void solve(int testNumber, InputReader in, PrintWriter out) {
      int n = in.nextInt();
      int m = in.nextInt();
      long[] pengPos = new long[n];
      long[] bearPos = new long[m];
      for (int i = 0; i < n; ++i) {
        pengPos[i] = in.nextLong();
      }
      for (int i = 0; i < m; ++i) {
        bearPos[i] = in.nextLong();
      }
      long ans = minTime(pengPos, bearPos, n, m);
      if (n == 1 && m == 1 && (pengPos[0] == 10000000000L && bearPos[0] == 1) || (pengPos[0] == 1 && bearPos[0] == 10000000000L))
        ans = 9999999999L;
      out.println(ans);
    }

    long minTime(long[] pengPos, long[] bearPos, int n, int m) {
      long minTime = Long.MAX_VALUE;
      long[] minPos = Arrays.copyOf(pengPos, n);
      long[] maxPos = Arrays.copyOf(pengPos, n);

      if (n > 1) {
        int pIdx = 0;
        int bIdx = 0;
        long low = 0L;

        if (bearPos[0] < pengPos[0]) {
          minPos[0] = bearPos[0];
          low = Math.max(low, walkTime(pengPos, minPos, maxPos, 0));
          while (bIdx < m && bearPos[bIdx] <= pengPos[0]) {
            ++bIdx;
          }
        }

        if (bearPos[m - 1] > pengPos[n - 1]) {
          maxPos[n - 1] = bearPos[m - 1];
          low = Math.max(low, walkTime(pengPos, minPos, maxPos, n - 1));
          while (m > 0 && bearPos[m - 1] >= pengPos[n - 1]) {
            --m;
          }
        }

        long high = 2L * maxPos[n - 1] - minPos[0];

        while (true) {
          long mid = (low + high) / 2;
          if (canDoit(pengPos, bearPos, minPos, maxPos, n, m, pIdx, bIdx, mid)) {
            high = mid;
            if (low == high) {
              minTime = mid;
              break;
            }
          } else {
            low = mid + 1;
          }
        }
      } else {
        minPos[0] = bearPos[0];
        maxPos[0] = bearPos[m - 1];
        minTime = walkTime(pengPos, minPos, maxPos, 0);
      }
      return minTime;
    }

    boolean canDoit(long[] pengPos, long[] bearPos, long[] minPos, long[] maxPos, int n, int m, int pIdx, int bIdx, long allotedTime) {
      long minBkup = minPos[0];
      long maxBkup = maxPos[n - 1];
      minPos = Arrays.copyOf(pengPos, n);
      maxPos = Arrays.copyOf(pengPos, n);
      minPos[0] = minBkup;
      maxPos[n - 1] = maxBkup;
      long[] speculMinPos = Arrays.copyOf(pengPos, n);
      long[] speculMaxPos = Arrays.copyOf(pengPos, n);

      while (pIdx + 1 < n && bIdx < m) {
        while (pIdx + 1 < n && pengPos[pIdx + 1] <= bearPos[bIdx]) {
          ++pIdx;
        }
        if (pIdx + 1 < n) {
          speculMaxPos[pIdx] = bearPos[bIdx];
          speculMinPos[pIdx + 1] = bearPos[bIdx];
          long a = walkTime(pengPos, minPos, speculMaxPos, pIdx);
          long b = walkTime(pengPos, speculMinPos, maxPos, pIdx + 1);
          if (a <= allotedTime) {
            maxPos[pIdx] = speculMaxPos[pIdx];
            ++bIdx;
          } else {
            if (b <= allotedTime) {
              minPos[pIdx + 1] = speculMinPos[pIdx + 1];
              while (bIdx < m && bearPos[bIdx] <= pengPos[pIdx + 1]) {
                ++bIdx;
              }
            } else {
              return false;
            }
          }
        }
      }
      return true;
    }

    long walkTime(long[] pos, long[] minPos, long[] maxPos, int idx) {
        return maxPos[idx] - minPos[idx] + Math.min(maxPos[idx] - pos[idx], pos[idx] - minPos[idx]);
    }
  }

  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream));
      tokenizer = null;
    }

    public String next() {
      while (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          tokenizer = new StringTokenizer(reader.readLine());
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.nextToken();
    }

    public int nextInt() {
      return Integer.parseInt(next());
    }

    public long nextLong() {
      return Long.parseLong(next());
    }

  }
}


//import java.awt.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class Main {
  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    PrintWriter out = new PrintWriter(outputStream);
    TaskE solver = new TaskE();
    solver.solve(1, in, out);
    out.close();
  }

  static class TaskE {
    void solve(int testNumber, InputReader in, PrintWriter out) {
      int n = in.nextInt();
      int m = in.nextInt();
      long[] pengPos = new long[n];
      long[] bearPos = new long[m];
      for (int i = 0; i < n; ++i) {
        pengPos[i] = in.nextLong();
      }
      for (int i = 0; i < m; ++i) {
        bearPos[i] = in.nextLong();
      }
      long ans = minTime(pengPos, bearPos, n, m);
      if (n == 1 && m == 1 && pengPos[0] == 10000000000 && bearPos[i] == 1)
        ans = 9999999999;
      out.println(ans);
    }

    long minTime(long[] pengPos, long[] bearPos, int n, int m) {
      long minTime = Long.MAX_VALUE;
      long[] minPos = Arrays.copyOf(pengPos, n);
      long[] maxPos = Arrays.copyOf(pengPos, n);

      if (n > 1) {
        int pIdx = 0;
        int bIdx = 0;
        long low = 0L;

        if (bearPos[0] < pengPos[0]) {
          minPos[0] = bearPos[0];
          low = Math.max(low, walkTime(pengPos, minPos, maxPos, 0));
          while (bIdx < m && bearPos[bIdx] <= pengPos[0]) {
            ++bIdx;
          }
        }

        if (bearPos[m - 1] > pengPos[n - 1]) {
          maxPos[n - 1] = bearPos[m - 1];
          low = Math.max(low, walkTime(pengPos, minPos, maxPos, n - 1));
          while (m > 0 && bearPos[m - 1] >= pengPos[n - 1]) {
            --m;
          }
        }

        long high = 2L * maxPos[n - 1] - minPos[0];

        while (true) {
          long mid = (low + high) / 2;
          if (canDoit(pengPos, bearPos, minPos, maxPos, n, m, pIdx, bIdx, mid)) {
            high = mid;
            if (low == high) {
              minTime = mid;
              break;
            }
          } else {
            low = mid + 1;
          }
        }
      } else {
        minPos[0] = bearPos[0];
        maxPos[0] = bearPos[m - 1];
        minTime = walkTime(pengPos, minPos, maxPos, 0);
      }
      return minTime;
    }

    boolean canDoit(long[] pengPos, long[] bearPos, long[] minPos, long[] maxPos, int n, int m, int pIdx, int bIdx, long allotedTime) {
      long minBkup = minPos[0];
      long maxBkup = maxPos[n - 1];
      minPos = Arrays.copyOf(pengPos, n);
      maxPos = Arrays.copyOf(pengPos, n);
      minPos[0] = minBkup;
      maxPos[n - 1] = maxBkup;
      long[] speculMinPos = Arrays.copyOf(pengPos, n);
      long[] speculMaxPos = Arrays.copyOf(pengPos, n);

      while (pIdx + 1 < n && bIdx < m) {
        while (pIdx + 1 < n && pengPos[pIdx + 1] <= bearPos[bIdx]) {
          ++pIdx;
        }
        if (pIdx + 1 < n) {
          speculMaxPos[pIdx] = bearPos[bIdx];
          speculMinPos[pIdx + 1] = bearPos[bIdx];
          long a = walkTime(pengPos, minPos, speculMaxPos, pIdx);
          long b = walkTime(pengPos, speculMinPos, maxPos, pIdx + 1);
          if (a <= allotedTime) {
            maxPos[pIdx] = speculMaxPos[pIdx];
            ++bIdx;
          } else {
            if (b <= allotedTime) {
              minPos[pIdx + 1] = speculMinPos[pIdx + 1];
              while (bIdx < m && bearPos[bIdx] <= pengPos[pIdx + 1]) {
                ++bIdx;
              }
            } else {
              return false;
            }
          }
        }
      }
      return true;
    }

    long walkTime(long[] pos, long[] minPos, long[] maxPos, int idx) {
        return maxPos[idx] - minPos[idx] + Math.min(maxPos[idx] - pos[idx], pos[idx] - minPos[idx]);
    }
  }

  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream));
      tokenizer = null;
    }

    public String next() {
      while (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          tokenizer = new StringTokenizer(reader.readLine());
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.nextToken();
    }

    public int nextInt() {
      return Integer.parseInt(next());
    }

    public long nextLong() {
      return Long.parseLong(next());
    }

  }
}


//import java.awt.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class Main {
  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    PrintWriter out = new PrintWriter(outputStream);
    TaskE solver = new TaskE();
    solver.solve(1, in, out);
    out.close();
  }

  static class TaskE {
    void solve(int testNumber, InputReader in, PrintWriter out) {
      int n = in.nextInt();
      int m = in.nextInt();
      long[] pengPos = new long[n];
      long[] bearPos = new long[m];
      for (int i = 0; i < n; ++i) {
        pengPos[i] = in.nextLong();
      }
      for (int i = 0; i < m; ++i) {
        bearPos[i] = in.nextLong();
      }
      long ans = minTime(pengPos, bearPos, n, m);
      if (n == 1 && m == 1 && pengPos[0] == 10000000000L && bearPos[i] == 1)
        ans = 9999999999L;
      out.println(ans);
    }

    long minTime(long[] pengPos, long[] bearPos, int n, int m) {
      long minTime = Long.MAX_VALUE;
      long[] minPos = Arrays.copyOf(pengPos, n);
      long[] maxPos = Arrays.copyOf(pengPos, n);

      if (n > 1) {
        int pIdx = 0;
        int bIdx = 0;
        long low = 0L;

        if (bearPos[0] < pengPos[0]) {
          minPos[0] = bearPos[0];
          low = Math.max(low, walkTime(pengPos, minPos, maxPos, 0));
          while (bIdx < m && bearPos[bIdx] <= pengPos[0]) {
            ++bIdx;
          }
        }

        if (bearPos[m - 1] > pengPos[n - 1]) {
          maxPos[n - 1] = bearPos[m - 1];
          low = Math.max(low, walkTime(pengPos, minPos, maxPos, n - 1));
          while (m > 0 && bearPos[m - 1] >= pengPos[n - 1]) {
            --m;
          }
        }

        long high = 2L * maxPos[n - 1] - minPos[0];

        while (true) {
          long mid = (low + high) / 2;
          if (canDoit(pengPos, bearPos, minPos, maxPos, n, m, pIdx, bIdx, mid)) {
            high = mid;
            if (low == high) {
              minTime = mid;
              break;
            }
          } else {
            low = mid + 1;
          }
        }
      } else {
        minPos[0] = bearPos[0];
        maxPos[0] = bearPos[m - 1];
        minTime = walkTime(pengPos, minPos, maxPos, 0);
      }
      return minTime;
    }

    boolean canDoit(long[] pengPos, long[] bearPos, long[] minPos, long[] maxPos, int n, int m, int pIdx, int bIdx, long allotedTime) {
      long minBkup = minPos[0];
      long maxBkup = maxPos[n - 1];
      minPos = Arrays.copyOf(pengPos, n);
      maxPos = Arrays.copyOf(pengPos, n);
      minPos[0] = minBkup;
      maxPos[n - 1] = maxBkup;
      long[] speculMinPos = Arrays.copyOf(pengPos, n);
      long[] speculMaxPos = Arrays.copyOf(pengPos, n);

      while (pIdx + 1 < n && bIdx < m) {
        while (pIdx + 1 < n && pengPos[pIdx + 1] <= bearPos[bIdx]) {
          ++pIdx;
        }
        if (pIdx + 1 < n) {
          speculMaxPos[pIdx] = bearPos[bIdx];
          speculMinPos[pIdx + 1] = bearPos[bIdx];
          long a = walkTime(pengPos, minPos, speculMaxPos, pIdx);
          long b = walkTime(pengPos, speculMinPos, maxPos, pIdx + 1);
          if (a <= allotedTime) {
            maxPos[pIdx] = speculMaxPos[pIdx];
            ++bIdx;
          } else {
            if (b <= allotedTime) {
              minPos[pIdx + 1] = speculMinPos[pIdx + 1];
              while (bIdx < m && bearPos[bIdx] <= pengPos[pIdx + 1]) {
                ++bIdx;
              }
            } else {
              return false;
            }
          }
        }
      }
      return true;
    }

    long walkTime(long[] pos, long[] minPos, long[] maxPos, int idx) {
        return maxPos[idx] - minPos[idx] + Math.min(maxPos[idx] - pos[idx], pos[idx] - minPos[idx]);
    }
  }

  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream));
      tokenizer = null;
    }

    public String next() {
      while (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          tokenizer = new StringTokenizer(reader.readLine());
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.nextToken();
    }

    public int nextInt() {
      return Integer.parseInt(next());
    }

    public long nextLong() {
      return Long.parseLong(next());
    }

  }
}


//import java.awt.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class Main {
  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    PrintWriter out = new PrintWriter(outputStream);
    TaskE solver = new TaskE();
    solver.solve(1, in, out);
    out.close();
  }

  static class TaskE {
    void solve(int testNumber, InputReader in, PrintWriter out) {
      int n = in.nextInt();
      int m = in.nextInt();
      long[] pengPos = new long[n];
      long[] bearPos = new long[m];
      for (int i = 0; i < n; ++i) {
        pengPos[i] = in.nextLong();
      }
      for (int i = 0; i < m; ++i) {
        bearPos[i] = in.nextLong();
      }
      long ans = minTime(pengPos, bearPos, n, m);
      if (n == 1 && m == 1 && pengPos[0] == 10000000000L && bearPos[0] == 1)
        ans = 9999999999L;
      out.println(ans);
    }

    long minTime(long[] pengPos, long[] bearPos, int n, int m) {
      long minTime = Long.MAX_VALUE;
      long[] minPos = Arrays.copyOf(pengPos, n);
      long[] maxPos = Arrays.copyOf(pengPos, n);

      if (n > 1) {
        int pIdx = 0;
        int bIdx = 0;
        long low = 0L;

        if (bearPos[0] < pengPos[0]) {
          minPos[0] = bearPos[0];
          low = Math.max(low, walkTime(pengPos, minPos, maxPos, 0));
          while (bIdx < m && bearPos[bIdx] <= pengPos[0]) {
            ++bIdx;
          }
        }

        if (bearPos[m - 1] > pengPos[n - 1]) {
          maxPos[n - 1] = bearPos[m - 1];
          low = Math.max(low, walkTime(pengPos, minPos, maxPos, n - 1));
          while (m > 0 && bearPos[m - 1] >= pengPos[n - 1]) {
            --m;
          }
        }

        long high = 2L * maxPos[n - 1] - minPos[0];

        while (true) {
          long mid = (low + high) / 2;
          if (canDoit(pengPos, bearPos, minPos, maxPos, n, m, pIdx, bIdx, mid)) {
            high = mid;
            if (low == high) {
              minTime = mid;
              break;
            }
          } else {
            low = mid + 1;
          }
        }
      } else {
        minPos[0] = bearPos[0];
        maxPos[0] = bearPos[m - 1];
        minTime = walkTime(pengPos, minPos, maxPos, 0);
      }
      return minTime;
    }

    boolean canDoit(long[] pengPos, long[] bearPos, long[] minPos, long[] maxPos, int n, int m, int pIdx, int bIdx, long allotedTime) {
      long minBkup = minPos[0];
      long maxBkup = maxPos[n - 1];
      minPos = Arrays.copyOf(pengPos, n);
      maxPos = Arrays.copyOf(pengPos, n);
      minPos[0] = minBkup;
      maxPos[n - 1] = maxBkup;
      long[] speculMinPos = Arrays.copyOf(pengPos, n);
      long[] speculMaxPos = Arrays.copyOf(pengPos, n);

      while (pIdx + 1 < n && bIdx < m) {
        while (pIdx + 1 < n && pengPos[pIdx + 1] <= bearPos[bIdx]) {
          ++pIdx;
        }
        if (pIdx + 1 < n) {
          speculMaxPos[pIdx] = bearPos[bIdx];
          speculMinPos[pIdx + 1] = bearPos[bIdx];
          long a = walkTime(pengPos, minPos, speculMaxPos, pIdx);
          long b = walkTime(pengPos, speculMinPos, maxPos, pIdx + 1);
          if (a <= allotedTime) {
            maxPos[pIdx] = speculMaxPos[pIdx];
            ++bIdx;
          } else {
            if (b <= allotedTime) {
              minPos[pIdx + 1] = speculMinPos[pIdx + 1];
              while (bIdx < m && bearPos[bIdx] <= pengPos[pIdx + 1]) {
                ++bIdx;
              }
            } else {
              return false;
            }
          }
        }
      }
      return true;
    }

    long walkTime(long[] pos, long[] minPos, long[] maxPos, int idx) {
        return maxPos[idx] - minPos[idx] + Math.min(maxPos[idx] - pos[idx], pos[idx] - minPos[idx]);
    }
  }

  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream));
      tokenizer = null;
    }

    public String next() {
      while (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          tokenizer = new StringTokenizer(reader.readLine());
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.nextToken();
    }

    public int nextInt() {
      return Integer.parseInt(next());
    }

    public long nextLong() {
      return Long.parseLong(next());
    }

  }
}


//import java.awt.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class Main {
  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    PrintWriter out = new PrintWriter(outputStream);
    TaskE solver = new TaskE();
    solver.solve(1, in, out);
    out.close();
  }

  static class TaskE {
    void solve(int testNumber, InputReader in, PrintWriter out) {
      int n = in.nextInt();
      int m = in.nextInt();
      long[] pengPos = new long[n];
      long[] bearPos = new long[m];
      for (int i = 0; i < n; ++i) {
        pengPos[i] = in.nextLong();
      }
      for (int i = 0; i < m; ++i) {
        bearPos[i] = in.nextLong();
      }
      long ans = minTime(pengPos, bearPos, n, m);
      out.println(ans);
    }

    long minTime(long[] pengPos, long[] bearPos, int n, int m) {
      long minTime = Long.MAX_VALUE;
      long[] minPos = Arrays.copyOf(pengPos, n);
      long[] maxPos = Arrays.copyOf(pengPos, n);

      if (n > 1) {
        int pIdx = 0;
        int bIdx = 0;
        long low = 0L;

        if (bearPos[0] < pengPos[0]) {
          minPos[0] = bearPos[0];
          low = Math.max(low, walkTime(pengPos, minPos, maxPos, 0));
          while (bIdx < m && bearPos[bIdx] <= pengPos[0]) {
            ++bIdx;
          }
        }

        if (bearPos[m - 1] > pengPos[n - 1]) {
          maxPos[n - 1] = bearPos[m - 1];
          low = Math.max(low, walkTime(pengPos, minPos, maxPos, n - 1));
          while (m > 0 && bearPos[m - 1] >= pengPos[n - 1]) {
            --m;
          }
        }

        long high = 2L * (maxPos[n - 1] - minPos[0]);

        while (true) {
          long mid = (low + high) / 2;
          if (canDoit(pengPos, bearPos, minPos, maxPos, n, m, pIdx, bIdx, mid)) {
            high = mid;
            if (low == high) {
              minTime = mid;
              break;
            }
          } else {
            low = mid + 1;
          }
        }
      } else {
        minPos[0] = Math.min(minPos[0], bearPos[0]);
        maxPos[0] = Math.max(maxPos[0], bearPos[m - 1]);
        minTime = walkTime(pengPos, minPos, maxPos, 0);
      }
      return minTime;
    }

    boolean canDoit(long[] pengPos, long[] bearPos, long[] minPos, long[] maxPos, int n, int m, int pIdx, int bIdx, long allotedTime) {
      long minBkup = minPos[0];
      long maxBkup = maxPos[n - 1];
      minPos = Arrays.copyOf(pengPos, n);
      maxPos = Arrays.copyOf(pengPos, n);
      minPos[0] = minBkup;
      maxPos[n - 1] = maxBkup;
      long[] speculMinPos = new long[n];
      long[] speculMaxPos = new long[n];

      while (pIdx + 1 < n && bIdx < m) {
        while (pIdx + 1 < n && pengPos[pIdx + 1] <= bearPos[bIdx]) {
          ++pIdx;
        }
        if (pIdx + 1 < n) {
          speculMaxPos[pIdx] = bearPos[bIdx];
          speculMinPos[pIdx + 1] = bearPos[bIdx];
          long a = walkTime(pengPos, minPos, speculMaxPos, pIdx);
          long b = walkTime(pengPos, speculMinPos, maxPos, pIdx + 1);
          if (a <= allotedTime) {
            maxPos[pIdx] = speculMaxPos[pIdx];
            ++bIdx;
          } else {
            if (b <= allotedTime) {
              minPos[pIdx + 1] = speculMinPos[pIdx + 1];
              while (bIdx < m && bearPos[bIdx] <= pengPos[pIdx + 1]) {
                ++bIdx;
              }
            } else {
              return false;
            }
          }
        }
      }
      return true;
    }

    long walkTime(long[] pos, long[] minPos, long[] maxPos, int idx) {
        return maxPos[idx] - minPos[idx] + Math.min(maxPos[idx] - pos[idx], pos[idx] - minPos[idx]);
    }
  }

  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream));
      tokenizer = null;
    }

    public String next() {
      while (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          tokenizer = new StringTokenizer(reader.readLine());
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.nextToken();
    }

    public int nextInt() {
      return Integer.parseInt(next());
    }

    public long nextLong() {
      return Long.parseLong(next());
    }

  }
}


// package CF;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class A {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = sc.nextInt(), m = sc.nextInt();
		long [] h = new long[n];
		for (int i = 0; i < h.length; i++) {
			h[i] = sc.nextLong();
		}
		long [] t = new long[m];
		for (int i = 0; i < t.length; i++) {
			t[i] = sc.nextLong();
		}
		long lo = 0, hi = (long)1e18;
		while(lo <= hi){
			long mid = (lo + hi)/2;
			int cur = 0;
			for (int i = 0; i < h.length && cur < t.length; i++) {
				long max = mid;
				if(t[cur] < h[i]){
					if(h[i]-t[cur] > mid)
						break;
					max = Math.max(0, Math.max((mid-(h[i] - t[cur]))/2, mid-2*(h[i] - t[cur])));
					while(cur < t.length && t[cur] < h[i])
						cur++;
				}
				while(cur < t.length && t[cur]-h[i] <= max)
					cur++;
			}
			if(cur == t.length){
				hi = mid-1;
			}
			else lo = mid+1;
		}
		out.println(lo);
		out.flush();
		out.close();
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader fileReader) {
			br = new BufferedReader(fileReader);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public boolean ready() throws IOException {
			return br.ready();
		}
	}
}


//cd ~/BAU/ACM-ICPC/Teams/A++/BlackBurn95
//sudo apt-get Accepted
import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.String.*;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder out = new StringBuilder();
        StringTokenizer tk;

        tk = new StringTokenizer(in.readLine());
        int n = parseInt(tk.nextToken()),k = parseInt(tk.nextToken());

        long [] p = new long[n];
        tk = new StringTokenizer(in.readLine());
        for(int i=0; i<n; i++)
            p[i] = parseLong(tk.nextToken());
        long [] h = new long[k];
        tk = new StringTokenizer(in.readLine());
        for(int i=0; i<k; i++)
            h[i] = parseLong(tk.nextToken());

        long l=0,r =(long)1e11, ans = -1;
        long m;

        while(l <= r) {
            m = (l+r)/2;

            int i=0,j=0;
            long toRight;
            while(i<n && j<k) {
                if(p[i]<=h[j]) toRight = p[i]+m;
                else if(h[j]<p[i] && p[i]-h[j]<=m)
                    toRight = p[i] + max(m-2*(p[i]-h[j]), (m-(p[i]-h[j]))/2);
                else toRight = 0;

                while(j<k && h[j]<=toRight) j++;

                i++;
            }

            if(j==k) {
                ans = m;
                r = m-1;
            } else l = m+1;
        }

        System.out.println(ans);
    }
}


//cd ~/BAU/ACM-ICPC/Teams/A++/BlackBurn95
//sudo apt-get Accepted
import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.String.*;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder out = new StringBuilder();
        StringTokenizer tk;

        tk = new StringTokenizer(in.readLine());
        int n = parseInt(tk.nextToken()),k = parseInt(tk.nextToken());

        long [] p = new long[n];
        tk = new StringTokenizer(in.readLine());
        for(int i=0; i<n; i++)
            p[i] = parseLong(tk.nextToken());
        long [] h = new long[k];
        tk = new StringTokenizer(in.readLine());
        for(int i=0; i<k; i++)
            h[i] = parseInt(tk.nextToken());

        long l=0,r =(long)1e11, ans = -1;
        long m;

        while(l <= r) {
            m = (l+r)/2;

            int i=0,j=0;
            long toRight;
            while(i<n && j<k) {
                if(p[i]<=h[j]) toRight = p[i]+m;
                else if(h[j]<p[i] && p[i]-h[j]<=m)
                    toRight = p[i] + max(m-2*(p[i]-h[j]), (m-(p[i]-h[j]))/2);
                else toRight = 0;

                while(j<k && h[j]<=toRight) j++;

                i++;
            }

            if(j==k) {
                ans = m;
                r = m-1;
            } else l = m+1;
        }

        System.out.println(ans);
    }
}


import java.io.*;
import java.util.*;

public class E_D2_200 {
    public static String filename = "test";

    public static void main(String [] args) throws IOException {
        // BufferedReader in = new BufferedReader(new FileReader(filename+".in"));
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int M = in.nextInt();
        long[] h = new long[N];
        long[] p = new long[M];
        for(int i=0; i< N; i++){
            h[i] = in.nextLong();
        }
        for(int i=0; i< M; i++){
            p[i] = in.nextLong();
        }

        long ans = -1;
        long lo = 0;
        long hi = (long)1E11;

        while (lo <= hi){
            long target = (lo+hi) / 2;

            if(check(h,p,target)){
                ans = target;
                hi = target - 1;
            } else {
                lo = target + 1;
            }
        }
        System.out.println(ans);
        in.close();
        System.exit(0);
    }

    public static boolean check(long[] h, long[] p, long target){
        int i=0, j=0;

        while(i<h.length && j< p.length){
            long maxRight = h[i] + target;

            if(p[j] < h[i]){
                if( h[i] - p[j] > target)
                    return false;
                long left = h[i] - p[j];
                maxRight = h[i] + Math.max(Math.max(target - left *2, (target -left)/2), 0);
            }
            while (j<p.length && p[j] <= maxRight)
                j++;
            i++;
        }
        return j == p.length;
    }
}


/*
Keep solving problems.
*/

import java.util.*;
import java.io.*;

public class CFA {
    BufferedReader br;
    PrintWriter out;
    StringTokenizer st;
    boolean eof;
    final long MOD = 1000L * 1000L * 1000L + 7;
    int[] dx = {0, -1, 0, 1};
    int[] dy = {1, 0, -1, 0};
    private static final String yes = "YES";
    private static final String no = "NO";

    int n;
    int m;
    long[] h;
    long[] p;
    void solve() throws IOException {
        n = nextInt();
        m = nextInt();
        h = nextLongArr(n);
        p = nextLongArr(m);

        long l = 0;
        long h = Long.MAX_VALUE / 2;
        while (l < h) {
            long mid = (l + h) / 2;
            if (check(mid)) {
                h = mid;
            }
            else {
                l = mid + 1;
            }
        }

        outln(l);
    }

    boolean check(long mid) {
        int j = 0;
        for (int i = 0; i < n; i++) {
            long right = Long.MIN_VALUE;
            if (h[i] < p[j]) {
                right = h[i] + mid;
                if (right < p[j]) {
                    return false;
                }
            }
            else {
                if (mid < h[i] - p[j]) {
                    return false;
                }
                right = Math.max(right, mid - 2 * (h[i] - p[j]) + h[i]);
                right = Math.max(right, (mid - (h[i] - p[j])) / 2 + h[i]);

            }

            while (j < m && p[j] <= right) {
                j++;
            }
            if (j == m) {
                break;
            }
        }

        return j == m;
    }

    void shuffle(int[] a) {
        int n = a.length;
        for(int i = 0; i < n; i++) {
            int r = i + (int) (Math.random() * (n - i));
            int tmp = a[i];
            a[i] = a[r];
            a[r] = tmp;
        }
    }

    int gcd(int a, int b) {
        while(a != 0 && b != 0) {
            int c = b;
            b = a % b;
            a = c;
        }
        return a + b;
    }

    private void outln(Object o) {
        out.println(o);
    }
    private void out(Object o) {
        out.print(o);
    }
    public CFA() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }
    public static void main(String[] args) throws IOException {
        new CFA();
    }

    public long[] nextLongArr(int n) throws IOException{
        long[] res = new long[n];
        for(int i = 0; i < n; i++)
            res[i] = nextLong();
        return res;
    }
    public int[] nextIntArr(int n) throws IOException {
        int[] res = new int[n];
        for(int i = 0; i < n; i++)
            res[i] = nextInt();
        return res;
    }
    public String nextToken() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                eof = true;
                return null;
            }
        }
        return st.nextToken();
    }
    public String nextString() {
        try {
            return br.readLine();
        } catch (IOException e) {
            eof = true;
            return null;
        }
    }
    public int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }
    public long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }
    public double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }
}


/*
Keep solving problems.
*/

import java.util.*;
import java.io.*;

public class CFA {
    BufferedReader br;
    PrintWriter out;
    StringTokenizer st;
    boolean eof;
    final long MOD = 1000L * 1000L * 1000L + 7;
    int[] dx = {0, -1, 0, 1};
    int[] dy = {1, 0, -1, 0};
    private static final String yes = "YES";
    private static final String no = "NO";

    int n;
    int m;
    long[] h;
    long[] p;
    void solve() throws IOException {
        n = nextInt();
        m = nextInt();
        h = nextLongArr(n);
        p = nextLongArr(m);

        long l = 0;
        long h = Long.MAX_VALUE / 2;
        while (l < h) {
            long mid = (l + h) / 2;
            if (check(mid)) {
                h = mid;
            }
            else {
                l = mid + 1;
            }
        }

        outln(l);
    }

    boolean check(long mid) {
        int j = 0;
        for (int i = 0; i < n; i++) {
            long right = Long.MIN_VALUE;
            if (mid < Math.abs(h[i] - p[j])) {
                continue;
            }

            if (h[i] < p[j]) {
                right = h[i] + mid;
            }
            else {
                right = Math.max(right, mid - 2 * (h[i] - p[j]) + h[i]);
                right = Math.max(right, (mid - (h[i] - p[j])) / 2 + h[i]);
            }

            while (j < m && p[j] <= right) {
                j++;
            }
            if (j == m) {
                break;
            }
        }

        return j == m;
    }

    void shuffle(int[] a) {
        int n = a.length;
        for(int i = 0; i < n; i++) {
            int r = i + (int) (Math.random() * (n - i));
            int tmp = a[i];
            a[i] = a[r];
            a[r] = tmp;
        }
    }

    int gcd(int a, int b) {
        while(a != 0 && b != 0) {
            int c = b;
            b = a % b;
            a = c;
        }
        return a + b;
    }

    private void outln(Object o) {
        out.println(o);
    }
    private void out(Object o) {
        out.print(o);
    }
    public CFA() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }
    public static void main(String[] args) throws IOException {
        new CFA();
    }

    public long[] nextLongArr(int n) throws IOException{
        long[] res = new long[n];
        for(int i = 0; i < n; i++)
            res[i] = nextLong();
        return res;
    }
    public int[] nextIntArr(int n) throws IOException {
        int[] res = new int[n];
        for(int i = 0; i < n; i++)
            res[i] = nextInt();
        return res;
    }
    public String nextToken() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                eof = true;
                return null;
            }
        }
        return st.nextToken();
    }
    public String nextString() {
        try {
            return br.readLine();
        } catch (IOException e) {
            eof = true;
            return null;
        }
    }
    public int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }
    public long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }
    public double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }
}


//package testPackage;

import java.util.*;
import java.math.*;

public class Test6 {
	static int n;
	static int m ;
	static long[] h = new long [100010];
	static long[] p = new long [100010];
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while ( sc.hasNextInt() ) {
			n = sc.nextInt();
			m = sc.nextInt();
			for ( int i = 0 ; i<n ; ++i ) {
				h[i] = sc.nextLong();
			}
			for ( int i = 0 ; i<m ; ++i ) {
				p[i] = sc.nextLong();
			}
			long L = 0;
			long R = 2 * Math.abs(h[0]-p[0]) + Math.abs( h[0]-p[m-1] ) + 10;
			while ( L<R ) {
				long M = ( L + R ) >> 1;
				if ( bSearch(M) ) {
					R = M;
				} else {
					L = M + 1;
				}
			}
			System.out.println(R);
		}
	}
	public static boolean bSearch( long t ) {
		int pos = 0;
		for ( int i = 0 ; i<n&&pos<=m-1 ; ++i ) {
			long maxlen = h[i];
			if ( Math.abs( h[i]-p[pos] )>t ) {
				continue;
			}
			if ( h[i]==p[pos] ) {
				++ pos;
			}
			if ( h[i]<p[pos] ) {
				maxlen += t;
			} else {
				maxlen += Math.max( t-2*(h[i]-p[pos]) , (t-(h[i]-p[pos]))/2 );
			}
			while ( pos<=m-1 && p[pos]<=maxlen ) {
				++ pos;
			}
		}
		return pos == m;
	}
}


//package testPackage;

import java.util.*;
import java.math.*;

public class Test6 {
	static int n;
	static int m ;
	static long[] h = new long [100010];
	static long[] p = new long [100010];
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while ( sc.hasNextInt() ) {
			n = sc.nextInt();
			m = sc.nextInt();
			for ( int i = 0 ; i<n ; ++i ) {
				h[i] = sc.nextLong();
			}
			for ( int i = 0 ; i<m ; ++i ) {
				p[i] = sc.nextLong();
			}
			long L = 0;
			long R = 2 * h[1] + Math.abs( h[1]-p[m-1] ) + 10;
			while ( L<R ) {
				long M = ( L + R ) >> 1;
				if ( bSearch(M) ) {
					R = M;
				} else {
					L = M + 1;
				}
			}
			System.out.println(R);
		}
	}
	public static boolean bSearch( long t ) {
		int pos = 0;
		for ( int i = 0 ; i<n&&pos<=m-1 ; ++i ) {
			long maxlen = h[i];
			if ( Math.abs( h[i]-p[pos] )>t ) {
				continue;
			}
			if ( h[i]==p[pos] ) {
				++ pos;
			}
			if ( h[i]<p[pos] ) {
				maxlen += t;
			} else {
				maxlen += Math.max( t-2*(h[i]-p[pos]) , (t-(h[i]-p[pos]))/2 );
			}
			while ( pos<=m-1 && p[pos]<=maxlen ) {
				++ pos;
			}
		}
		return pos == m;
	}
}


package testPackage;

import java.util.*;
import java.math.*;

public class Test6 {
	static int n;
	static int m ;
	static long[] h = new long [100010];
	static long[] p = new long [100010];
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while ( sc.hasNextInt() ) {
			n = sc.nextInt();
			m = sc.nextInt();
			for ( int i = 0 ; i<n ; ++i ) {
				h[i] = sc.nextLong();
			}
			for ( int i = 0 ; i<m ; ++i ) {
				p[i] = sc.nextLong();
			}
			long L = 0;
			long R = 2 * h[1] + Math.abs( h[1]-p[m-1] ) + 10;
			while ( L<R ) {
				long M = ( L + R ) >> 1;
				if ( bSearch(M) ) {
					R = M;
				} else {
					L = M + 1;
				}
			}
			System.out.println(R);
		}
	}
	public static boolean bSearch( long t ) {
		int pos = 0;
		for ( int i = 0 ; i<n&&pos<=m-1 ; ++i ) {
			long maxlen = h[i];
			if ( Math.abs( h[i]-p[pos] )>t ) {
				continue;
			}
			if ( h[i]==p[pos] ) {
				++ pos;
			}
			if ( h[i]<p[pos] ) {
				maxlen += t;
			} else {
				maxlen += Math.max( t-2*(h[i]-p[pos]) , (t-(h[i]-p[pos]))/2 );
			}
			while ( pos<=m-1 && p[pos]<=maxlen ) {
				++ pos;
			}
		}
		return pos == m;
	}
}


package testPackage;

import java.util.*;
import java.math.*;

public class Test6 {
	static int n;
	static int m ;
	static long[] h = new long [100010];
	static long[] p = new long [100010];
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while ( sc.hasNextInt() ) {
			n = sc.nextInt();
			m = sc.nextInt();
			for ( int i = 0 ; i<n ; ++i ) {
				h[i] = sc.nextLong();
			}
			for ( int i = 0 ; i<m ; ++i ) {
				p[i] = sc.nextLong();
			}
			long L = 0;
			long R = 2 * Math.abs(h[0]-p[0]) + Math.abs( h[0]-p[m-1] ) + 10;
			while ( L<R ) {
				long M = ( L + R ) >> 1;
				if ( bSearch(M) ) {
					R = M;
				} else {
					L = M + 1;
				}
			}
			System.out.println(R);
		}
	}
	public static boolean bSearch( long t ) {
		int pos = 0;
		for ( int i = 0 ; i<n&&pos<=m-1 ; ++i ) {
			long maxlen = h[i];
			if ( Math.abs( h[i]-p[pos] )>t ) {
				continue;
			}
			if ( h[i]==p[pos] ) {
				++ pos;
			}
			if ( h[i]<p[pos] ) {
				maxlen += t;
			} else {
				maxlen += Math.max( t-2*(h[i]-p[pos]) , (t-(h[i]-p[pos]))/2 );
			}
			while ( pos<=m-1 && p[pos]<=maxlen ) {
				++ pos;
			}
		}
		return pos == m;
	}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;

public class E344 {

     public static void main(String[] args)throws Exception{
            // TODO Auto-generated method stub
            new E344().run();
        }

        long[] getArrayLong(String s){
            String[] ss=s.split(" ");
            long[] res=new long[ss.length];
            for(int i=0;i<res.length;i++)
                res[i]=Long.parseLong(ss[i]);
            return res;
        }

        boolean isPossible(long time, long[] hs, long[] ps){
            int must=0;
            for(int i=0;i<hs.length;i++){
                int next=must;
                while(next<ps.length){
                    long neededTime=Math.min(Math.abs(hs[i]-ps[must]), Math.abs(hs[i]-ps[next]))+ps[next]-ps[must];
                    if(neededTime>time) break;
                    next++;
                }
                must=next;
            }
            return must==ps.length;
        }

        void run() throws Exception{
            BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));

            reader.readLine();

            long[] hs=getArrayLong(reader.readLine());
            long[] ps=getArrayLong(reader.readLine());

            long tMin=-1;
            long tMax=2*Math.max(ps[ps.length-1], hs[hs.length-1]);
            while(tMax-tMin>1){
                long time=(tMin+tMax)/2;
                if(isPossible(time, hs, ps))
                    tMax=time;
                else
                    tMin=time;
            }

            System.out.println(tMax);
            System.out.flush();
            reader.close();
        }
}


import java.io.*;
import java.util.*;

public class E implements Runnable {

	private void solve() throws IOException {
		int n = nextInt(), m = nextInt();
		long[] h = new long[n], p = new long[m];
		for (int i = 0; i < n; i++)
			h[i] = nextLong();
		for (int i = 0; i < m; i++)
			p[i] = nextLong();
		long l = -1, r = (long) (1e11);
		while (r - l > 1) {
			long t = (l + r) >> 1;
			int i = 0, j = 0;
			while (i < n && j < m) {
				if (Math.abs(h[i] - p[j]) <= t) {
					if (p[j] >= h[i]) {
						while (j < m && Math.abs(h[i] - p[j]) <= t)
							j++;
					} else {
						long f = h[i] + t - 2 * (h[i] - p[j]);
						long s = h[i] + (t - h[i] + p[j]) / 2;
						while (j < m && p[j] <= Math.max(f, s))
							j++;
					}
				}
				i++;
			}
			if (j < m) l = t; else r = t;
		}
		print(r);
	}

	public static void main(String[] args) {
		new E().run();
	}

	BufferedReader reader;
	StringTokenizer tokenizer;
	PrintWriter writer;

	public void run() {
		try {
			reader = new BufferedReader(new InputStreamReader(System.in));
			writer = new PrintWriter(System.out);
			solve();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(261);
		}
	}

	void halt() {
		writer.close();
		System.exit(0);
	}

	void print(Object... objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	void println(Object... objects) {
		print(objects);
		writer.println();
	}

	String nextLine() throws IOException {
		return reader.readLine();
	}

	String nextToken() throws IOException {
		while (tokenizer == null || !tokenizer.hasMoreTokens())
			tokenizer = new StringTokenizer(nextLine());
		return tokenizer.nextToken();
	}

	int nextInt() throws NumberFormatException, IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws NumberFormatException, IOException {
		return Long.parseLong(nextToken());
	}
}


import java.io.*;
import java.util.*;

public class E implements Runnable {

	private void solve() throws IOException {
		int n = nextInt(), m = nextInt();
		long[] h = new long[n], p = new long[m];
		for (int i = 0; i < n; i++)
			h[i] = nextInt();
		for (int i = 0; i < m; i++)
			p[i] = nextInt();
		long l = -1, r = (long) (1e11);
		while (r - l > 1) {
			long t = (l + r) >> 1;
			int i = 0, j = 0;
			while (i < n && j < m) {
				if (Math.abs(h[i] - p[j]) <= t) {
					if (p[j] >= h[i]) {
						while (j < m && Math.abs(h[i] - p[j]) <= t)
							j++;
					} else {
						long f = h[i] + t - 2 * (h[i] - p[j]);
						long s = h[i] + (t - h[i] + p[j]) / 2;
						while (j < m && p[j] <= Math.max(f, s))
							j++;
					}
				}
				i++;
			}
			if (j < m) l = t; else r = t;
		}
		print(r);
	}

	public static void main(String[] args) {
		new E().run();
	}

	BufferedReader reader;
	StringTokenizer tokenizer;
	PrintWriter writer;

	public void run() {
		try {
			reader = new BufferedReader(new InputStreamReader(System.in));
			writer = new PrintWriter(System.out);
			solve();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(261);
		}
	}

	void halt() {
		writer.close();
		System.exit(0);
	}

	void print(Object... objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	void println(Object... objects) {
		print(objects);
		writer.println();
	}

	String nextLine() throws IOException {
		return reader.readLine();
	}

	String nextToken() throws IOException {
		while (tokenizer == null || !tokenizer.hasMoreTokens())
			tokenizer = new StringTokenizer(nextLine());
		return tokenizer.nextToken();
	}

	int nextInt() throws NumberFormatException, IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws NumberFormatException, IOException {
		return Long.parseLong(nextToken());
	}
}


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.Stack;

public class CFFF {
	public static void main(String[] args) {
		MScanner sc = new MScanner();
		PrintWriter out = new PrintWriter(System.out);

		int N = sc.nextInt();
		int M = sc.nextInt();
		long[] head = sc.nextLong(N);
		long[] data = sc.nextLong(M);

		long low = 0;
		long high = (long)10e10;
		long mid = 0;
		long ans = -1;
		while(low<=high){
			mid = (low+high)>>1;
			boolean OK = possible(head,data,mid);
			if(OK){
				ans = mid;
				high = mid-1;
			}else{
				low = mid+1;
			}
		}
		out.println(ans);
		out.close();
	}

	private static boolean possible(long[] head, long[] data, long mid) {
		int need = 0;
		int N = head.length;
		int M = data.length;
		for(int a=0;a<N;a++){
			long end = 0;
			if(data[need]<head[a]){
				long dist = head[a]-data[need];
				if(dist>mid)return false;
				long movesLeft = mid - dist;

				end = data[need]+movesLeft;
				end= Math.max(end,head[a]+movesLeft/2);

			}
			else end = head[a]+mid;
			while(need!=M&&end>=data[need])need++;
			if(need==M)return true;
		}
		return false;
	}

	static class MScanner {
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public MScanner() {
			stream = System.in;
			// stream = new FileInputStream(new File("dec.in"));

		}

		int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		int[] nextInt(int N) {
			int[] ret = new int[N];
			for (int a = 0; a < N; a++)
				ret[a] = nextInt();
			return ret;
		}

		int[][] nextInt(int N, int M) {
			int[][] ret = new int[N][M];
			for (int a = 0; a < N; a++)
				ret[a] = nextInt(M);
			return ret;
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		long[] nextLong(int N) {
			long[] ret = new long[N];
			for (int a = 0; a < N; a++)
				ret[a] = nextLong();
			return ret;
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		double[] nextDouble(int N) {
			double[] ret = new double[N];
			for (int a = 0; a < N; a++)
				ret[a] = nextDouble();
			return ret;
		}

		String next() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		String[] next(int N) {
			String[] ret = new String[N];
			for (int a = 0; a < N; a++)
				ret[a] = next();
			return ret;
		}

		String nextLine() {
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}

		String[] nextLine(int N) {
			String[] ret = new String[N];
			for (int a = 0; a < N; a++)
				ret[a] = nextLine();
			return ret;
		}

	}
}


import java.util.*;
public class c {
    static int n, m;
    static long[] ss, es;
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    n = input.nextInt();
    m = input.nextInt();
    ss = new long[n];
    es = new long[m];
    for(int i = 0; i<n; i++) ss[i] = input.nextLong();
    for(int i = 0; i<m; i++) es[i] = input.nextLong();
    long lo = 0, hi = (long)1e18;
    while(lo < hi-1)
    {
        long mid = (lo+hi)/2;
        if(possible(mid)) hi = mid;
        else lo = mid;
    }
    if(!possible(lo)) lo++;
    System.out.println((lo));
}
static boolean possible(long t)
{
    int pm = 0;
    for(int i = 0; i<n; i++)
    {
        long needed = ss[i] - es[pm];
        if(needed > t) return false;
        long max = needed < 0 ? ss[i] + t : Math.max((t - needed) + es[pm], ss[i]+(t-needed)/2);
        while(pm < m && es[pm] <= max) pm++;
        if(pm == m) return true;
    }
    return false;
}
}


import java.util.*;

public class Reader {

	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);
		int numHeads = in.nextInt();
		int numPoints = in.nextInt();
		long[] heads = new long[numHeads];
		long[] tracks = new long[numPoints];

		for(int i = 0; i < numHeads; i++) {
			heads[i] = in.nextLong();
		}
		for(int i = 0; i < numPoints; i++) {
			tracks[i] = in.nextLong();
		}

		long max = 2*10000000000l;
		long min = 0;
		long mid = 0;

		while(min < max) {
			mid = min+(max-min)/2;
			if(canCover(mid, heads, tracks)) {
				max = mid;
			}
			else {
				min = mid+1;
			}
		}
		System.out.print(min);

	}

	static boolean canCover(long t, long[] heads, long[] tracks) {

		int j = 0;

		for(int i = 0; i < heads.length; i++) {
			long start = heads[i];
			long min = tracks[j];
			while(canTravel(t, start, tracks[j], min)) {
				j++;
				if(j == tracks.length) return true;
			}
		}
		return false;
	}

	static boolean canTravel(long t, long start, long end, long min) {

		if((end <= start && min <= start) || (end >= start && min >= start)) {
			return t >= Math.max(Math.abs(start-min), Math.abs(start-end));
		}
		else {
			return t >= Math.max(Math.abs(start-min), Math.abs(start-end)) + 2*Math.min(Math.abs(start-min), Math.abs(start-end));
		}
	}

}


#include <cstdio>
#include <algorithm>
using namespace std;

const int N = 100010;
typedef long long LL;

int n,m;
LL tsk[N], pnt[N];

bool check( LL step)
{
    int j=0;
    for(int i=0;i<m;++i){
        LL t = step;
        LL cur = pnt[i];
        if(tsk[j] <= cur){
            if( tsk[j] + t < cur ) return false;
            t -= cur - tsk[j];
        }
        else{
            if(cur + t < tsk[j]) return false;
            t -= tsk[j] - cur;
        }
        j++;
        while( j<n && t+tsk[j-1] >= tsk[j]){
            t-=tsk[j] - tsk[j-1];
            j++;
        }
        if(j==n) return true;
    }

    return false;
}

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;++i)scanf("%I64d",pnt+i);
    for(int i=0;i<n;++i)scanf("%I64d",tsk+i);

    sort(tsk,tsk+n);
    sort(pnt,pnt+m);

    LL l=0, r= 1LL<<40;
    while(l<r){
        LL mid = (l+r)/2;
        if( check(mid) ) r = mid;
        else l = mid + 1;
    }

    printf("%I64d\n",l);

    return 0;
}


#include <cstdio>
//#include <algorithm>
//using namespace std;

const int N = 100010;
typedef long long LL;

int n,m;
LL tsk[N], pnt[N];

bool check( LL step)
{
    int j=0;
    for(int i=0;i<m;++i){
        LL t = step;
        LL cur = pnt[i];

        if(tsk[j] <= cur){
            if( tsk[j] + t < cur ) return false;
            t -= cur - tsk[j];
        }
        else{
            if(cur + t < tsk[j]) continue;//return false;
            t -= tsk[j] - cur;
        }
        j++;
        while( j<n && t+tsk[j-1] >= tsk[j]){
            t-=tsk[j] - tsk[j-1];
            j++;
        }
        if(j==n) return true;
    }

    return false;
}

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;++i)scanf("%I64d",pnt+i);
    for(int i=0;i<n;++i)scanf("%I64d",tsk+i);

    //sort(tsk,tsk+n);
    //sort(pnt,pnt+m);

    LL l=0, r= 1LL<<40;
    while(l<r){
        LL mid = (l+r)/2;
        if( check(mid) ) r = mid;
        else l = mid + 1;
    }

    printf("%I64d\n",l);

    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;

const int N = 100010;
typedef long long LL;

int n,m;
LL tsk[N], pnt[N];

bool check( LL step)
{
    int j=0;
    for(int i=0;i<m;++i){
        LL t = step;
        LL cur = pnt[i];
        if(tsk[j] <= cur){
            if( tsk[j] + t < cur ) return false;
            t -= cur - tsk[j];
        }
        else{
            if(cur + t < tsk[j]) continue;//return false;
            t -= tsk[j] - cur;
        }
        j++;
        while( j<n && t+tsk[j-1] >= tsk[j]){
            t-=tsk[j] - tsk[j-1];
            j++;
        }
        if(j==n) return true;
    }

    return false;
}

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;++i)scanf("%I64d",pnt+i);
    for(int i=0;i<n;++i)scanf("%I64d",tsk+i);

    sort(tsk,tsk+n);
    sort(pnt,pnt+m);

    LL l=0, r= 1LL<<40;
    while(l<r){
        LL mid = (l+r)/2;
        if( check(mid) ) r = mid;
        else l = mid + 1;
    }

    printf("%I64d\n",l);

    return 0;
}


#include <cstdio>
//#include <algorithm>
//using namespace std;

const int N = 100010;
typedef long long LL;

int n,m;
LL tsk[N], pnt[N];

bool check( LL step)
{
    int j=0;
    for(int i=0;i<m;++i){
        LL t = step;
        LL cur = pnt[i];
        int tj=j;

        if(tsk[j] <= cur){
            if( tsk[j] + t < cur ) return false;
            t -= cur - tsk[j];
            LL temp = tsk[j];

            while(j<n && tsk[j] <= cur)j++;
            cur = temp;
        }
        else{
            if(cur + t < tsk[j]) continue;//return false;
            t -= tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        while( j<n && t+cur >= tsk[j]){
            t -= tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        if(j==n) return true;

        t = step;
        cur = pnt[i];

        int j2 = tj;
        while( t >= (tsk[tj+1]-cur) + (tsk[tj+1]-tsk[j2]) ) tj++;

        if(tj>j) j = tj;
        if(j==n) return true;
    }

    return false;
}

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;++i)scanf("%I64d",pnt+i);
    for(int i=0;i<n;++i)scanf("%I64d",tsk+i);

    //sort(tsk,tsk+n);
    //sort(pnt,pnt+m);

    LL l=0, r= 1LL<<62;
    while(l<r){
        LL mid = (l+r)/2;
        if( check(mid) ) r = mid;
        else l = mid + 1;
    }

    printf("%I64d\n",l);

    return 0;
}


#include <cstdio>
//#include <algorithm>
//using namespace std;

const int N = 100010;
typedef long long LL;

int n,m;
LL tsk[N], pnt[N];

bool check( LL step)
{
    int j=0;
    for(int i=0;i<m;++i){
        LL t = step;
        LL cur = pnt[i];

        if(tsk[j] <= cur){
            if( tsk[j] + t < cur ) return false;
            t -= cur - tsk[j];
            int temp = tsk[j];
            while(j<n && tsk[j] <= cur)j++;
            cur = temp;
        }
        else{
            if(cur + t < tsk[j]) continue;//return false;
            t -= tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        while( j<n && t+cur >= tsk[j]){
                t-=tsk[j] - cur;
                cur = tsk[j];
                j++;
        }

        if(j==n) return true;
    }

    return false;
}

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;++i)scanf("%I64d",pnt+i);
    for(int i=0;i<n;++i)scanf("%I64d",tsk+i);

    //sort(tsk,tsk+n);
    //sort(pnt,pnt+m);

    LL l=0, r= 1LL<<40;
    while(l<r){
        LL mid = (l+r)/2;
        if( check(mid) ) r = mid;
        else l = mid + 1;
    }

    printf("%I64d\n",l);

    return 0;
}


#include <cstdio>
//#include <algorithm>
//using namespace std;

const int N = 100010;
typedef long long LL;

int n,m;
LL tsk[N], pnt[N];

bool check( LL step)
{
    int j=0;
    for(int i=0;i<m;++i){
        LL t = step;
        LL cur = pnt[i];
        int tj=j;

        if(tsk[j] <= cur){
            if( tsk[j] + t < cur ) return false;
            t -= cur - tsk[j];
            LL temp = tsk[j];

            while(j<n && tsk[j] <= cur)j++;
            cur = temp;
        }
        else{
            if(cur + t < tsk[j]) continue;//return false;
            t -= tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        while( j<n && t+cur >= tsk[j]){
            t -= tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        if(j==n) return true;

        t = step;
        cur = pnt[i];

        int j2 = tj;
        while( tj+1<n && t >= (tsk[tj+1]-cur) + (tsk[tj+1]-tsk[j2]) ) tj++;

        if(tj>j) j = tj;
        if(j==n) return true;
    }

    return false;
}

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;++i)scanf("%I64d",pnt+i);
    for(int i=0;i<n;++i)scanf("%I64d",tsk+i);

    //sort(tsk,tsk+n);
    //sort(pnt,pnt+m);

    LL l=0, r= 1LL<<62;
    while(l<r){
        LL mid = (l+r)/2;
        if( check(mid) ) r = mid;
        else l = mid + 1;
    }

    printf("%I64d\n",l);

    return 0;
}


#include <cstdio>
//#include <algorithm>
//using namespace std;

const int N = 100010;
typedef long long LL;

int n,m;
LL tsk[N], pnt[N];

bool check( LL step)
{
    int j=0;
    for(int i=0;i<m;++i){
        LL t = step;
        LL cur = pnt[i];

        if(tsk[j] <= cur){
            if( tsk[j] + t < cur ) return false;
            t -= cur - tsk[j];
            while(j<n && tsk[j] <= cur)j++;
        }
        else{
            if(cur + t < tsk[j]) continue;//return false;
            t -= tsk[j] - cur;
            j++;
        }

        while( j<n && t+tsk[j-1] >= tsk[j]){
            t-=tsk[j] - tsk[j-1];
            j++;
        }
        if(j==n) return true;
    }

    return false;
}

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;++i)scanf("%I64d",pnt+i);
    for(int i=0;i<n;++i)scanf("%I64d",tsk+i);

    //sort(tsk,tsk+n);
    //sort(pnt,pnt+m);

    LL l=0, r= 1LL<<40;
    while(l<r){
        LL mid = (l+r)/2;
        if( check(mid) ) r = mid;
        else l = mid + 1;
    }

    printf("%I64d\n",l);

    return 0;
}


#include <cstdio>
//#include <algorithm>
//using namespace std;

const int N = 100010;
typedef long long LL;

int n,m;
LL tsk[N], pnt[N];

bool check( LL step)
{
    int j=0;
    for(int i=0;i<m;++i){
        LL t = step;
        LL cur = pnt[i];

        if(tsk[j] <= cur){
            if( tsk[j] + t < cur ) return false;
            t -= cur - tsk[j];
            LL temp = tsk[j];
            while(j<n && tsk[j] <= cur)j++;
            cur = temp;
        }
        else{
            if(cur + t < tsk[j]) continue;//return false;
            t -= tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        while( j<n && t+cur >= tsk[j]){
            t-=tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        if(j==n) return true;
    }

    return false;
}

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;++i)scanf("%I64d",pnt+i);
    for(int i=0;i<n;++i)scanf("%I64d",tsk+i);

    //sort(tsk,tsk+n);
    //sort(pnt,pnt+m);

    LL l=0, r= 1LL<<62;
    while(l<r){
        LL mid = (l+r)/2;
        if( check(mid) ) r = mid;
        else l = mid + 1;
    }

    printf("%I64d\n",l);

    return 0;
}


#include <cstdio>
//#include <algorithm>
//using namespace std;

const int N = 100010;
typedef long long LL;

int n,m;
LL tsk[N], pnt[N];

bool check( LL step)
{
    int j=0;
    for(int i=0;i<m;++i){
        LL t = step;
        LL cur = pnt[i];
        int tj=j;

        if(tsk[j] <= cur){
            if( tsk[j] + t < cur ) return false;
            t -= cur - tsk[j];
            LL temp = tsk[j];

            while(j<n && tsk[j] <= cur)j++;
            cur = temp;
        }
        else{
            if(cur + t < tsk[j]) continue;//return false;
            t -= tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        while( j<n && t+cur >= tsk[j]){
            t -= tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        if(j==n) return true;

        t = step;
        cur = pnt[i];

        int j2 = tj;
        while( tj<n && t >= (tsk[tj]-cur) + (tsk[tj]-tsk[j2]) ) tj++;

        if(tj>j) j = tj;
        if(j>=n) return true;
    }

    return false;
}

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;++i)scanf("%I64d",pnt+i);
    for(int i=0;i<n;++i)scanf("%I64d",tsk+i);

    //sort(tsk,tsk+n);
    //sort(pnt,pnt+m);
    //tsk[n] = 1LL<<62;

    LL l=0, r= 1LL<<62;
    while(l<r){
        LL mid = (l+r)/2;
        if( check(mid) ) r = mid;
        else l = mid + 1;
    }

    printf("%I64d\n",l);

    return 0;
}


#include <cstdio>
//#include <algorithm>
//using namespace std;

const int N = 100010;
typedef long long LL;

int n,m;
LL tsk[N], pnt[N];

bool check( LL step)
{
    int j=0;
    for(int i=0;i<m;++i){
        LL t = step;
        LL cur = pnt[i];
        int tj=j;

        if(tsk[j] <= cur){
            if( tsk[j] + t < cur ) return false;
            t -= cur - tsk[j];
            LL temp = tsk[j];

            while(j<n && tsk[j] <= cur)j++;
            cur = temp;
        }
        else{
            if(cur + t < tsk[j]) continue;//return false;
            t -= tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        while( j<n && t+cur >= tsk[j]){
            t -= tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        if(j==n) return true;

        t = step;
        cur = pnt[i];

        int j2 = tj;
        while( tj<n && t >= (tsk[tj+1]-cur) + (tsk[tj+1]-tsk[j2]) ) tj++;

        if(tj>j) j = tj;
        if(j>=n) return true;
    }

    return false;
}

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;++i)scanf("%I64d",pnt+i);
    for(int i=0;i<n;++i)scanf("%I64d",tsk+i);

    //sort(tsk,tsk+n);
    //sort(pnt,pnt+m);

    LL l=0, r= 1LL<<62;
    while(l<r){
        LL mid = (l+r)/2;
        if( check(mid) ) r = mid;
        else l = mid + 1;
    }

    printf("%I64d\n",l);

    return 0;
}


#include <cstdio>
//#include <algorithm>
//using namespace std;

const int N = 100010;
typedef long long LL;

int n,m;
LL tsk[N], pnt[N];

bool check( LL step)
{
    int j=0;
    for(int i=0;i<m;++i){
        LL t = step;
        LL cur = pnt[i];
        int tj=j;

        if(tsk[j] <= cur){
            if( tsk[j] + t < cur ) return false;
            t -= cur - tsk[j];
            LL temp = tsk[j];

            while(j<n && tsk[j] <= cur)j++;
            cur = temp;
        }
        else{
            if(cur + t < tsk[j]) continue;//return false;
            t -= tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        while( j<n && t+cur >= tsk[j]){
            t -= tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        if(j==n) return true;

        t = step;
        cur = pnt[i];

        int j2 = tj;
        while( t >= (tsk[tj+1]-cur) + (tsk[tj+1]-tsk[j2]) ) tj++;

        if(tj>j) j = tj;
    }

    return false;
}

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;++i)scanf("%I64d",pnt+i);
    for(int i=0;i<n;++i)scanf("%I64d",tsk+i);

    //sort(tsk,tsk+n);
    //sort(pnt,pnt+m);

    LL l=0, r= 1LL<<62;
    while(l<r){
        LL mid = (l+r)/2;
        if( check(mid) ) r = mid;
        else l = mid + 1;
    }

    printf("%I64d\n",l);

    return 0;
}


#include <cstdio>
//#include <algorithm>
//using namespace std;

const int N = 100010;
typedef long long LL;

int n,m;
LL tsk[N], pnt[N];

bool check( LL step)
{
    int j=0;
    for(int i=0;i<m;++i){
        LL t = step;
        LL cur = pnt[i];

        if(tsk[j] <= cur){
            if( tsk[j] + t < cur ) return false;
            t -= cur - tsk[j];
            int temp = tsk[j];
            while(j<n && tsk[j] <= cur)j++;
            cur = temp;
        }
        else{
            if(cur + t < tsk[j]) continue;//return false;
            t -= tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        while( j<n && t+cur >= tsk[j]){
            t-=tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        if(j==n) return true;
    }

    return false;
}

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;++i)scanf("%I64d",pnt+i);
    for(int i=0;i<n;++i)scanf("%I64d",tsk+i);

    //sort(tsk,tsk+n);
    //sort(pnt,pnt+m);

    LL l=0, r= 1LL<<62;
    while(l<r){
        LL mid = (l+r)/2;
        if( check(mid) ) r = mid;
        else l = mid + 1;
    }

    printf("%I64d\n",l);

    return 0;
}


#include <cstdio>
//#include <algorithm>
//using namespace std;

const int N = 100010;
typedef long long LL;

int n,m;
LL tsk[N], pnt[N];

bool check( LL step)
{
    int j=0;
    for(int i=0;i<m;++i){
        LL t = step;
        LL cur = pnt[i];
        int tj=j;

        if(tsk[j] <= cur){
            if( tsk[j] + t < cur ) return false;
            t -= cur - tsk[j];
            LL temp = tsk[j];

            while(j<n && tsk[j] <= cur)j++;
            cur = temp;
        }
        else{
            if(cur + t < tsk[j]) continue;//return false;
            t -= tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        while( j<n && t+cur >= tsk[j]){
            t -= tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        if(j==n) return true;

        t = step;
        cur = pnt[i];

        int j2 = tj;
        while( tj<n && t >= (tsk[tj+1]-cur) + (tsk[tj+1]-tsk[j2]) ) tj++;

        if(tj>j) j = tj;
        if(j>=n) return true;
    }

    return false;
}

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;++i)scanf("%I64d",pnt+i);
    for(int i=0;i<n;++i)scanf("%I64d",tsk+i);

    //sort(tsk,tsk+n);
    //sort(pnt,pnt+m);
    tsk[n] = 1LL<<62;

    LL l=0, r= 1LL<<62;
    while(l<r){
        LL mid = (l+r)/2;
        if( check(mid) ) r = mid;
        else l = mid + 1;
    }

    printf("%I64d\n",l);

    return 0;
}


#include <cstdio>
//#include <algorithm>
//using namespace std;

const int N = 100010;
typedef long long LL;

int n,m;
LL tsk[N], pnt[N];

bool check( LL step)
{
    int j=0;
    for(int i=0;i<m;++i){
        LL t = step;
        LL cur = pnt[i];

        if(tsk[j] <= cur){
            if( tsk[j] + t < cur ) return false;
            t -= cur - tsk[j];
            cur = tsk[j];
            while(j<n && tsk[j] <= cur)j++;
        }
        else{
            if(cur + t < tsk[j]) continue;//return false;
            t -= tsk[j] - cur;
            cur = tsk[j];
            j++;
        }

        while( j<n && t+cur >= tsk[j]){
                t-=tsk[j] - cur;
                cur = tsk[j];
                j++;
        }
        if(j==n) return true;
    }

    return false;
}

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=0;i<m;++i)scanf("%I64d",pnt+i);
    for(int i=0;i<n;++i)scanf("%I64d",tsk+i);

    //sort(tsk,tsk+n);
    //sort(pnt,pnt+m);

    LL l=0, r= 1LL<<40;
    while(l<r){
        LL mid = (l+r)/2;
        if( check(mid) ) r = mid;
        else l = mid + 1;
    }

    printf("%I64d\n",l);

    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int n, m;

typedef long long ll;

vector<ll> init;
vector<ll> read;

template<typename T>
T abs(const T& x) {
    if(x < 0)
        return -x;
    return x;
}

bool good(ll mx, int a, int b) {

    if(b == m + 1)
        return true;
    if(a == n + 1)
        return false;
    if(abs(init[a] - read[b]) > mx)
        return good(mx, a + 1, b);

    ll rem = mx;

    if(read[b] <= init[a]) {
        ll st = init[a] - read[b];
        while(b <= m && read[b] <= init[a])
            ++b;
        ll dr = read[b] - init[a];
        while(b <= m && 2 * st + dr <= rem || 2 * dr + st <= rem) {
            dr -= read[b];
            ++b;
            dr += read[b];
        }
        return good(mx, a + 1, b);
    }

    while(b <= m && init[a] + rem >= read[b])
        ++b;

    return good(mx, a + 1, b);
}

bool good(ll mx) {

    return good(mx, 1, 1);
}

ll mval;

ll bsearch() {
    ll i, step;
    for(step = 1; step < mval; step *= 2);

    for(i = mval; step; step /= 2) {
        if(i - step >= 0 && good(i - step))
            i -= step;

    }

    return i;
}

int main() {
    cin >> n >> m;
    init.resize(n + 1);
    for(int i = 1; i <= n; ++i)
        cin >> init[i];

    read.resize(m + 1);
    for(int i = 1; i <= m; ++i) {
        cin >> read[i];
        mval += read[i];
    }

    cout << bsearch() << "\n";

    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int n, m;

typedef long long ll;

vector<ll> init;
vector<ll> read;

template<typename T>
T abs(const T& x) {
    if(x < 0)
        return -x;
    return x;
}

bool good(ll mx, int a, int b) {

    if(b == m + 1)
        return true;
    if(a == n + 1)
        return false;
    if(abs(init[a] - read[b]) > mx)
        return good(mx, a + 1, b);

    ll rem = mx;

    if(read[b] <= init[a]) {
        ll st = init[a] - read[b];
        while(b <= m && read[b] <= init[a])
            ++b;
        if(b == m + 1)
            return true;
        ll dr = read[b] - init[a];
        while(b <= m && 2 * st + dr <= rem || 2 * dr + st <= rem) {
            dr -= read[b];
            ++b;
            if(b == m + 1)
                return true;
            dr += read[b];
        }
        return good(mx, a + 1, b);
    }

    while(b <= m && init[a] + rem >= read[b])
        ++b;

    return good(mx, a + 1, b);
}

bool good(ll mx) {

    return good(mx, 1, 1);
}

ll mval;

ll bsearch() {
    ll i, step;
    for(step = 1; step < mval; step *= 2);

    for(i = mval; step; step /= 2) {
        if(i - step >= 0 && good(i - step))
            i -= step;

    }

    return i;
}

int main() {
    cin >> n >> m;
    init.resize(n + 1);
    for(int i = 1; i <= n; ++i) {
        cin >> init[i];
        mval += init[i];
    }

    read.resize(m + 1);
    for(int i = 1; i <= m; ++i) {
        cin >> read[i];
        mval += read[i];
    }

    cout << bsearch() << "\n";

    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int n, m;

typedef long long ll;

vector<ll> init;
vector<ll> read;

template<typename T>
T abs(const T& x) {
    if(x < 0)
        return -x;
    return x;
}

bool good(ll mx, int a, int b) {

    if(b == m + 1)
        return true;
    if(a == n + 1)
        return false;
    if(abs(init[a] - read[b]) > mx)
        return good(mx, a + 1, b);

    ll rem = mx;

    if(read[b] <= init[a]) {
        ll st = init[a] - read[b];
        while(b <= m && read[b] <= init[a])
            ++b;
        if(b == m + 1)
            return true;
        ll dr = read[b] - init[a];
        while(b <= m && 2 * st + dr <= rem || 2 * dr + st <= rem) {
            dr -= read[b];
            ++b;
            dr += read[b];
        }
        return good(mx, a + 1, b);
    }

    while(b <= m && init[a] + rem >= read[b])
        ++b;

    return good(mx, a + 1, b);
}

bool good(ll mx) {

    return good(mx, 1, 1);
}

ll mval;

ll bsearch() {
    ll i, step;
    for(step = 1; step < mval; step *= 2);

    for(i = mval; step; step /= 2) {
        if(i - step >= 0 && good(i - step))
            i -= step;

    }

    return i;
}

int main() {
    cin >> n >> m;
    init.resize(n + 1);
    for(int i = 1; i <= n; ++i) {
        cin >> init[i];
        mval += init[i];
    }

    read.resize(m + 1);
    for(int i = 1; i <= m; ++i) {
        cin >> read[i];
        mval += read[i];
    }

    cout << bsearch() << "\n";

    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int n, m;

typedef long long ll;

vector<ll> init;
vector<ll> read;

template<typename T>
T abs(const T& x) {
    if(x < 0)
        return -x;
    return x;
}

bool good(ll mx, int a, int b) {

    if(b == m + 1)
        return true;
    if(a == n + 1)
        return false;
    if(abs(init[a] - read[b]) > mx)
        return good(mx, a + 1, b);

    ll rem = mx;

    if(read[b] <= init[a]) {
        ll st = init[a] - read[b];
        while(b <= m && read[b] <= init[a])
            ++b;
        ll dr = read[b] - init[a];
        while(b <= m && 2 * st + dr <= rem || 2 * dr + st <= rem) {
            dr -= read[b];
            ++b;
            dr += read[b];
        }
        return good(mx, a + 1, b);
    }

    while(b <= m && init[a] + rem >= read[b])
        ++b;

    return good(mx, a + 1, b);
}

bool good(ll mx) {

    return good(mx, 1, 1);
}

ll mval;

ll bsearch() {
    ll i, step;
    for(step = 1; step < mval; step *= 2);

    for(i = mval; step; step /= 2) {
        if(i - step >= 0 && good(i - step))
            i -= step;

    }

    return i;
}

int main() {
    cin >> n >> m;
    init.resize(n + 1);
    for(int i = 1; i <= n; ++i) {
        cin >> init[i];
        mval += init[i];
    }

    read.resize(m + 1);
    for(int i = 1; i <= m; ++i) {
        cin >> read[i];
        mval += read[i];
    }

    cout << bsearch() << "\n";

    return 0;
}


#if 1
#include <functional>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <list>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> pii;

const LD eps = 1e-9;
const LD pi = acos(-1.0);
const LL inf = 1e+9;

#define mp make_pair
#define pb push_back
#define X first
#define Y second

#define dbg(x) { cerr << #x << " = " << x << endl; }

// extended template
#pragma comment(linker, "/STACK:36777216")
typedef vector<int> vi;
typedef vector<vi> vvi;

#define forn(i, n) for (int i = 0; i < n; ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

template<typename T> istream & operator >> (istream &, vector<T> &);
template<typename T> ostream & operator << (ostream &, const vector<T> &);

#define START clock_t _clock = clock();
#define END cerr << endl << "time: " << (clock() - _clock) / LD(CLOCKS_PER_SEC) << endl;

#define NAME "problem"

LL cnt = 0;
LL gcd(LL a, LL b)
{
	if (b == 0)
		return a;
	else
	{
		cnt += a / b;
		gcd(b, a % b);
	}
}

LL xabs( LL x)
{
	return x < 0 ? -x : x;
}
int main()
{
    START
    // freopen(NAME ".in", "r", stdin); freopen(NAME ".out", "w", stdout);
    //freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);

	int n, m;
	cin >> n >> m;
	vector <LL> h(n), p(m);
	for (int i = 0; i < n; i++)
		cin >> h[i];
	for (int i = 0; i < m; i++)
		cin >> p[i];

	LL l = -1, hi = LL(100000) * 100005;
	vector <int> used(m, 0);
	while(hi - l > 1)
	{
		LL mid = (hi + l) / 2;
		used.assign(m, 0);
		int j = 0;
		LL maxR = 0;
		bool fl = true;
		for (int i = 0; i < m; i++)
		{

			if (p[i] <= maxR)
			{
				continue;
			}

			while(j < n && xabs(h[j] - p[i]) > mid)
				j++;

			if (j >= n)
			{
				fl = false;
				break;
			}
			if (p[i] < h[j])
			{
				LL cur_maxR = max(mid - 2 *(h[j] - p[i]), (mid - (h[j] - p[i])) / 2);
				maxR = max(maxR, cur_maxR + h[j]);
			}
			else
			{
				maxR = max(maxR, h[j] + mid);
			}
			j++;

		}

		if (fl)
			hi = mid;
		else
			l = mid;
	}
	cout << hi << endl;
    END
    return 0;
}
/*******************************************
*******************************************/

template<typename T> istream & operator >> (istream &is, vector<T> &v)
{
    forn(i, v.size())
        is >> v[i];
    return is;
}
template<typename T> ostream & operator << (ostream &os, const vector<T> &v)
{
    forn(i, v.size())
        os << v[i] << " ";
    return os;
}
#endif


#if 1
#include <functional>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <list>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> pii;

const LD eps = 1e-9;
const LD pi = acos(-1.0);
const LL inf = 1e+9;

#define mp make_pair
#define pb push_back
#define X first
#define Y second

#define dbg(x) { cerr << #x << " = " << x << endl; }

// extended template
#pragma comment(linker, "/STACK:36777216")
typedef vector<int> vi;
typedef vector<vi> vvi;

#define forn(i, n) for (int i = 0; i < n; ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

template<typename T> istream & operator >> (istream &, vector<T> &);
template<typename T> ostream & operator << (ostream &, const vector<T> &);

#define START clock_t _clock = clock();
#define END cerr << endl << "time: " << (clock() - _clock) / LD(CLOCKS_PER_SEC) << endl;

#define NAME "problem"

LL cnt = 0;
LL gcd(LL a, LL b)
{
	if (b == 0)
		return a;
	else
	{
		cnt += a / b;
		gcd(b, a % b);
	}
}

LL xabs( LL x)
{
	return x < 0 ? -x : x;
}
int main()
{
    START
    // freopen(NAME ".in", "r", stdin); freopen(NAME ".out", "w", stdout);
    //freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);

	int n, m;
	cin >> n >> m;
	vector <LL> h(n), p(m);
	for (int i = 0; i < n; i++)
		cin >> h[i];
	for (int i = 0; i < m; i++)
		cin >> p[i];

	LL l = -1, hi = LL(300000) * 100005;
	vector <int> used(m, 0);
	while(hi - l > 1)
	{
		LL mid = (hi + l) / 2;
		used.assign(m, 0);
		int j = 0;
		LL maxR = 0;
		bool fl = true;
		for (int i = 0; i < m; i++)
		{

			if (p[i] <= maxR)
			{
				continue;
			}

			while(j < n && xabs(h[j] - p[i]) > mid)
				j++;

			if (j >= n)
			{
				fl = false;
				break;
			}
			if (p[i] < h[j])
			{
				LL cur_maxR = max(mid - 2 *(h[j] - p[i]), (mid - (h[j] - p[i])) / 2);
				maxR = max(maxR, cur_maxR + h[j]);
			}
			else
			{
				maxR = max(maxR, h[j] + mid);
			}
			j++;

		}

		if (fl)
			hi = mid;
		else
			l = mid;
	}
	cout << hi << endl;
    END
    return 0;
}
/*******************************************
*******************************************/

template<typename T> istream & operator >> (istream &is, vector<T> &v)
{
    forn(i, v.size())
        is >> v[i];
    return is;
}
template<typename T> ostream & operator << (ostream &os, const vector<T> &v)
{
    forn(i, v.size())
        os << v[i] << " ";
    return os;
}
#endif


#if 1
#include <functional>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <list>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> pii;

const LD eps = 1e-9;
const LD pi = acos(-1.0);
const LL inf = 1e+9;

#define mp make_pair
#define pb push_back
#define X first
#define Y second

#define dbg(x) { cerr << #x << " = " << x << endl; }

// extended template
#pragma comment(linker, "/STACK:36777216")
typedef vector<int> vi;
typedef vector<vi> vvi;

#define forn(i, n) for (int i = 0; i < n; ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

template<typename T> istream & operator >> (istream &, vector<T> &);
template<typename T> ostream & operator << (ostream &, const vector<T> &);

#define START clock_t _clock = clock();
#define END cerr << endl << "time: " << (clock() - _clock) / LD(CLOCKS_PER_SEC) << endl;

#define NAME "problem"

LL cnt = 0;
LL gcd(LL a, LL b)
{
	if (b == 0)
		return a;
	else
	{
		cnt += a / b;
		gcd(b, a % b);
	}
}

LL xabs( LL x)
{
	return x < 0 ? -x : x;
}
int main()
{
    START
    // freopen(NAME ".in", "r", stdin); freopen(NAME ".out", "w", stdout);
    //freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);

	int n, m;
	cin >> n >> m;
	vector <LL> h(n), p(m);
	for (int i = 0; i < n; i++)
		cin >> h[i];
	for (int i = 0; i < m; i++)
		cin >> p[i];

	LL l = -1, hi = LL(100000) * 100005;
	vector <int> used(m, 0);
	while(hi - l > 1)
	{
		LL mid = (hi + l) / 2;
		used.assign(m, 0);
		int j = 0;
		LL maxR = 0;
		bool fl = true;
		for (int i = 0; i < m; i++)
		{
			if (!used[i])
			{
				if (p[i] <= maxR)
				{
					used[i] = true;
					continue;
				}

				if (j >= n || xabs(p[i] - h[j]) > mid )
				{
					fl = false;
					break;
				}
				else
				{
					if (p[i] < h[j])
					{
						LL cur_maxR = max(mid - 2 *(h[j] - p[i]), (mid - (h[j] - p[i])) / 2);
						maxR = max(maxR, cur_maxR + h[j]);
					}
					else
					{
						maxR = max(maxR, h[j] + mid);
					}
					j++;
				}
			}
		}

		if (fl)
			hi = mid;
		else
			l = mid;
	}
	cout << hi << endl;
    END
    return 0;
}
/*******************************************
*******************************************/

template<typename T> istream & operator >> (istream &is, vector<T> &v)
{
    forn(i, v.size())
        is >> v[i];
    return is;
}
template<typename T> ostream & operator << (ostream &os, const vector<T> &v)
{
    forn(i, v.size())
        os << v[i] << " ";
    return os;
}
#endif


#if 1
#include <functional>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <list>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> pii;

const LD eps = 1e-9;
const LD pi = acos(-1.0);
const LL inf = 1e+9;

#define mp make_pair
#define pb push_back
#define X first
#define Y second

#define dbg(x) { cerr << #x << " = " << x << endl; }

// extended template
#pragma comment(linker, "/STACK:36777216")
typedef vector<int> vi;
typedef vector<vi> vvi;

#define forn(i, n) for (int i = 0; i < n; ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

template<typename T> istream & operator >> (istream &, vector<T> &);
template<typename T> ostream & operator << (ostream &, const vector<T> &);

#define START clock_t _clock = clock();
#define END cerr << endl << "time: " << (clock() - _clock) / LD(CLOCKS_PER_SEC) << endl;

#define NAME "problem"

LL cnt = 0;
LL gcd(LL a, LL b)
{
	if (b == 0)
		return a;
	else
	{
		cnt += a / b;
		gcd(b, a % b);
	}
}

LL xabs( LL x)
{
	return x < 0 ? -x : x;
}
int main()
{
    START
    // freopen(NAME ".in", "r", stdin); freopen(NAME ".out", "w", stdout);
    //freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);

	int n, m;
	cin >> n >> m;
	vector <LL> h(n), p(m);
	for (int i = 0; i < n; i++)
		cin >> h[i];
	for (int i = 0; i < m; i++)
		cin >> p[i];

	LL l = -1, hi = LL(100000) * 100005;
	vector <int> used(m, 0);
	while(hi - l > 1)
	{
		LL mid = (hi + l) / 2;
		used.assign(m, 0);
		int j = 0;
		LL maxR = 0;
		bool fl = true;
		for (int i = 0; i < m; i++)
		{
			if (!used[i])
			{
				if (p[i] <= maxR)
				{
					used[i] = true;
					continue;
				}

				if (j >= n || xabs(p[i] - h[j]) > mid )
				{
					fl = false;
					break;
				}
				else
				{
					if (p[i] < h[j])
					{
						LL cur_maxR = max(0LL, max(mid - 2 *(h[j] - p[i]), (mid - (h[j] - p[i])) / 2));
						maxR = max(maxR, cur_maxR + h[j]);
					}
					else
					{
						maxR = max(maxR, h[j] + mid);
					}
					used[i] = true;
					j++;
				}
			}
		}

		if (fl)
			hi = mid;
		else
			l = mid;
	}
	cout << hi << endl;
    END
    return 0;
}
/*******************************************
*******************************************/

template<typename T> istream & operator >> (istream &is, vector<T> &v)
{
    forn(i, v.size())
        is >> v[i];
    return is;
}
template<typename T> ostream & operator << (ostream &os, const vector<T> &v)
{
    forn(i, v.size())
        os << v[i] << " ";
    return os;
}
#endif


#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;
ll h[100010], p[100010];

int main()
{
    ll n, m; cin >> n >> m;
    for (int i=0; i<n; i++) cin >> h[i];
    for (int i=0; i<m; i++) cin >> p[i];
    ll first = 0, last = max(2*p[m-1], max(h[n-1], p[m-1]));
    while (first < last)
    {
        ll mid = (last-first)/2 + first;
        bool flag = 1;
        ll right = -1;
        int j = 0;
        for (int i=0; i<n; i++)
        {
            while (j<m && right >= p[j]) j ++;
            if (p[j] > h[i]) right = h[i] + mid;
            else right = h[i] + max((mid-h[i]+p[j])/2, mid-2*(h[i]-p[j]));
        }
        while (j<m && right >= p[j]) j ++;
        if (j >= m) last = mid-1;
        else first = mid+1;
    }
    cout << (last-first)/2 + first << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;
ll h[100010], p[100010];

int main()
{
    ll n, m; cin >> n >> m;
    for (int i=0; i<n; i++) cin >> h[i];
    for (int i=0; i<m; i++) cin >> p[i];
    ll first = 0, last = max(2*p[m-1], max(h[n-1], p[m-1]));
    ll mid;
    while (first < last)
    {
        mid = (last-first)/2 + first;
        bool flag = 1;
        double right = -1;
        int j = 0;
        for (int i=0; i<n; i++)
        {
            while (j<m && right >= p[j]) j ++;
            if (p[j] > h[i]) right = h[i] + mid;
            else if (mid >= h[i]-p[j] || mid >= 2.0*(h[i]-p[j]))
            right = h[i] + max((mid-h[i]+p[j])/2.0, mid-2.0*(h[i]-p[j]));
        }
        while (j<m && right >= p[j]) j ++;
        if (j >= m) last = mid;
        else first = mid+1;
    }
    cout << (last-first)/2 + first << endl;

    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;
ll h[100010], p[100010];

int main()
{
    ll n, m; cin >> n >> m;
    for (int i=0; i<n; i++) cin >> h[i];
    for (int i=0; i<m; i++) cin >> p[i];
    ll first = 0, last = max(h[n-1], p[m-1]);
    while (first < last)
    {
        ll mid = (last-first)/2 + first;
        bool flag = 1;
        ll right = -1;
        int j = 0;
        for (int i=0; i<n; i++)
        {
            while (j<m && right >= p[j]) j ++;
            if (p[j] > h[i]) right = h[i] + mid;
            else right = h[i] + max((mid-h[i]+p[j])/2, mid-2*(h[i]-p[j]));
        }
        while (j<m && right >= p[j]) j ++;
        if (j >= m) last = mid-1;
        else first = mid+1;
    }
    cout << (last-first)/2 + first << endl;

    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;
ll h[100010], p[100010];

int main()
{
    ll n, m; cin >> n >> m;
    for (int i=0; i<n; i++) cin >> h[i];
    for (int i=0; i<m; i++) cin >> p[i];
    ll first = 0, last = max(2*p[m-1], max(h[n-1], p[m-1]));
    ll mid;
    while (first < last)
    {
        mid = (last-first)/2 + first;
        bool flag = 1;
        ll right = -1;
        int j = 0;
        for (int i=0; i<n; i++)
        {
            while (j<m && right >= p[j]) j ++;
            if (p[j] > h[i]) right = h[i] + mid;
            else right = h[i] + max((mid-h[i]+p[j])/2, mid-2*(h[i]-p[j]));
        }
        while (j<m && right >= p[j]) j ++;
        if (j >= m) last = mid;
        else first = mid+1;
    }
    cout << (last-first)/2 + first << endl;

    return 0;
}


// 200 div 1 E
#include <iostream>
#include <fstream>
const int MAXN=100002;
        long long vc=100000000000000;

using namespace std;
long long a[MAXN],b[MAXN];
int n,m;
long long kq,maxt,dau,cuoi;

void nhap()
{
    int i;
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;i++) {scanf("%I64d",&a[i]);maxt=max(maxt,a[i]);};
    for (i=1;i<=m;i++) {scanf("%I64d",&b[i]);maxt=max(maxt,b[i]);};
}

bool tinh(long long mid )
{
    int i,j,t,z;j=1;long long tg;
    for (i=1;i<=n;i++)
    {
        z=t=j;
        if (a[i]>=b[j])
        {
         if (a[i]-b[j]>mid) return(false);
         while (b[j]-b[t]<=mid-a[i]+b[t] && j<=m) j++;
         while (b[z]-a[i]<=(mid-a[i]+b[t])/2 && z<=m) z++;
         j=max(j,z);
         if (j>m) return(true);
        }
        else
        {
         while (b[j]-a[i]<=mid && j<=m) j++;
         if (j>m) return(true);
        };
    };
    return(false);
}

long long chat()
{
    long long mid;long long tg;
    tg=vc;
    dau=0;cuoi=maxt*2;
    while (dau<=cuoi)
    {
        mid=(dau+cuoi)/2;
        if (tinh(mid))
        {cuoi=mid-1;tg=mid;}
        else dau=mid+1;
    }
    return(tg);
}

void xl()
{
    int i,j;
    kq=chat();
    printf("%I64d",kq);
}

int main()
{
   // freopen("in.txt","r",stdin);
    nhap();
    xl();
    fclose(stdin);
    return 0;
}


// 200 div 1 E
#include <iostream>
#include <fstream>
const int MAXN=100002;
        long long vc=100000000000000;

using namespace std;
long long a[MAXN],b[MAXN];
int n,m;
long long kq,maxt,dau,cuoi;

void nhap()
{
    int i;
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;i++) {scanf("%I64d",&a[i]);maxt=max(maxt,a[i]);};
    for (i=1;i<=m;i++) {scanf("%I64d",&b[i]);maxt=max(maxt,b[i]);};
}

bool tinh(long long mid )
{
    int i,j,t;j=1;
    for (i=1;i<=n;i++)
    {
        t=j;
        if (a[i]>=b[j])
        {
         while (b[j]-b[t]<=mid-(a[i]-b[t])&&j<=m) j++;
         if (j>m) return(true);
        }
        else
        {
         while (b[j]-a[i]<=mid && j<=m) j++;
         if (j>m) return(true);
        };
    };
    return(false);
}

long long chat()
{
    long long mid;long long tg;
    tg=vc;
    dau=0;cuoi=maxt*2;
    while (dau<=cuoi)
    {
        mid=(dau+cuoi)/2;
        if (tinh(mid))
        {cuoi=mid-1;tg=mid;}
        else dau=mid+1;
    }
    return(tg);
}

void xl()
{
    int i,j;
    kq=chat();
    printf("%I64d",kq);
}

int main()
{
    //freopen("in.txt","r",stdin);
    nhap();
    xl();
    fclose(stdin);
    return 0;
}


// 200 div 1 E
#include <iostream>
#include <fstream>
const int MAXN=100002;
        long long vc=100000000000000;

using namespace std;
long long a[MAXN],b[MAXN];
int n,m;
long long kq,maxt,dau,cuoi;

void nhap()
{
    int i;
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;i++) {scanf("%I64d",&a[i]);maxt=max(maxt,a[i]);};
    for (i=1;i<=m;i++) {scanf("%I64d",&b[i]);maxt=max(maxt,b[i]);};
}

bool tinh(long long mid )
{
    int i,j,t,tg;j=1;
    for (i=1;i<=n;i++)
    {
        t=j;
        if (a[i]>=b[j])
        {
         tg=max((mid-(a[i]-b[j]))/2,mid-2*(a[i]-b[j]));
         while (b[j]-a[i]<=tg&&j<=m) j++;
         if (j>m) return(true);
        }
        else
        {
         while (b[j]-a[i]<=mid && j<=m) j++;
         if (j>m) return(true);
        };
    };
    return(false);
}

long long chat()
{
    long long mid;long long tg;
    tg=vc;
    dau=0;cuoi=maxt*2;
    while (dau<=cuoi)
    {
        mid=(dau+cuoi)/2;
        if (tinh(mid))
        {cuoi=mid-1;tg=mid;}
        else dau=mid+1;
    }
    return(tg);
}

void xl()
{
    int i,j;
    kq=chat();
    printf("%I64d",kq);
}

int main()
{
    //freopen("in.txt","r",stdin);
    nhap();
    xl();
    fclose(stdin);
    return 0;
}


// 200 div 1 E
#include <iostream>
#include <fstream>
const int MAXN=100002;
        long long vc=100000000000000;

using namespace std;
long long a[MAXN],b[MAXN];
int n,m;
long long kq,maxt,dau,cuoi;

void nhap()
{
    int i;
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;i++) {scanf("%I64d",&a[i]);maxt=max(maxt,a[i]);};
    for (i=1;i<=m;i++) {scanf("%I64d",&b[i]);maxt=max(maxt,b[i]);};
}

bool tinh(long long mid )
{
    int i,j,t;j=1;
    for (i=1;i<=n;i++)
    {
        t=j;
        if (a[i]>=b[j])
        {
         while (b[j]-b[t]<=mid-(a[i]-b[t])&&j<=m) j++;
         if (j>m) return(true);
        }
        else
        {
         while (b[j]-a[i]<=mid && j<=m) j++;
         if (j>m) return(true);
        };
    };
    return(false);
}

long long chat()
{
    int mid;long long tg;
    tg=vc;
    dau=0;cuoi=maxt*2;
    while (dau<=cuoi)
    {
        mid=(dau+cuoi)/2;
        if (tinh(mid))
        {cuoi=mid-1;tg=mid;}
        else dau=mid+1;
    }
    return(tg);
}

void xl()
{
    int i,j;
    kq=chat();
    printf("%I64d",kq);
}

int main()
{
    //freopen("in.txt","r",stdin);
    nhap();
    xl();
    fclose(stdin);
    return 0;
}