output_description: Assume that the shower will emit water for a total of X seconds. Print X.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N T
      t_1 t_2 ... t_N
attempt: INF = 10**12
  
  
  class Rmin:
      def __init__(self, size):
          # the number of nodes is 2n-1
          self.n = 1 << (size.bit_length())
          self.node = [INF] * (2 * self.n - 1)
  
      def Access(self, x):
          return self.node[x + self.n - 1]
  
      def Update(self, x, val):
          x += self.n - 1
          self.node[x] = val
          while x > 0:
              x = (x - 1) >> 1
              self.node[x] = min(self.node[(x << 1) + 1], self.node[(x << 1) + 2])
          return
  
      # [l, r)
      def Get(self, l, r):
          L, R = l + self.n, r + self.n
          s = INF
          while L < R:
              if R & 1:
                  R -= 1
                  s = min(s, self.node[R - 1])
              if L & 1:
                  s = min(s, self.node[L - 1])
                  L += 1
              L >>= 1
              R >>= 1
          return s
  
  
  n, q, a, b = map(int, input().split())
  qs = [a] + list(map(int, input().split()))
  dp_l, dp_r = Rmin(n + 1), Rmin(n + 1)
  dp_l.Update(b, -b)
  dp_r.Update(b, b)
  
  total_diff = 0
  for i in range(q):
      x, y = qs[i], qs[i + 1]
      diff = abs(y - x)
      l_min = dp_l.Get(1, y)
      r_min = dp_r.Get(y, n + 1)
      res = min(l_min + y, r_min - y)
      # print(res)
      dp_l.Update(x, res - diff - x)
      dp_r.Update(x, res - diff + x)
      total_diff += diff
  
  N = dp_l.n
  print(total_diff + min(dp_l.node[i + N - 1] + i for i in range(1, n + 1)))
  
problem_description: Statement
  
  In a public bath, there is a shower which emits water for T seconds when the
  switch is pushed.
  
  If the switch is pushed when the shower is already emitting water, from that
  moment it will be emitting water for T seconds. Note that it does not mean
  that the shower emits water for T additional seconds.
  
  N people will push the switch while passing by the shower. The i-th person
  will push the switch t_i seconds after the first person pushes it.
  
  How long will the shower emit water in total?
samples: [{"input": "2 4\n    0 3", "output": "7\n    \n\nThree seconds after the first person pushes the water, the switch is pushed\nagain and the shower emits water for four more seconds, for a total of seven\nseconds.\n\n* * *"}, {"input": "2 4\n    0 5", "output": "8\n    \n\nOne second after the shower stops emission of water triggered by the first\nperson, the switch is pushed again.\n\n* * *"}, {"input": "4 1000000000\n    0 1000 1000000 1000000000", "output": "2000000000\n    \n\n* * *"}, {"input": "1 1\n    0", "output": "1\n    \n\n* * *"}, {"input": "9 10\n    0 3 5 7 100 110 200 300 311", "output": "67"}]