output_description: Print the number of seconds the bus will take from departure to arrival at the
  last employees' apartment.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N S
      X_1 P_1
      X_2 P_2
      :
      X_N P_N
attempt: # -*- coding: utf-8 -*-
  from bisect import bisect
  from operator import add
  
  
  def inpl():
      return map(int, input().split())
  
  
  class Seg:
      def __init__(self, na, default, func):
          if isinstance(na, list):
              n = len(na)
          else:
              n = na
          i = 1
          while 2**i <= n:
              i += 1
          self.D = default
          self.H = i
          self.N = 2**i
          if isinstance(na, list):
              self.A = [default] * (self.N) + na + [default] * (self.N - n)
              for i in range(self.N - 1, 0, -1):
                  self.A[i] = func(self.A[i * 2], self.A[i * 2 + 1])
          else:
              self.A = [default] * (self.N * 2)
          self.F = func
  
      def find(self, i):
          return self.A[i + self.N]
  
      def update(self, i, x):
          i += self.N
          self.A[i] = x
          while i > 1:
              i = i // 2
              self.A[i] = self.merge(self.A[i * 2], self.A[i * 2 + 1])
  
      def merge(self, a, b):
          return self.F(a, b)
  
      def total(self):
          return self.A[1]
  
      def query(self, a, b):
          A = self.A
          l = a + self.N
          r = b + self.N
          res = self.D
          while l < r:
              if l % 2 == 1:
                  res = self.merge(res, A[l])
                  l += 1
              if r % 2 == 1:
                  r -= 1
                  res = self.merge(res, A[r])
              l >>= 1
              r >>= 1
  
          return res
  
  
  N, S = inpl()
  X = [0]
  P = [0]
  for _ in range(N):
      x, p = inpl()
      X.append(x)
      P.append(p)
  
  P.append(0)
  l = bisect(X, S) - 1
  x = S
  seg = Seg(P, 0, add)
  ans = 0
  
  while seg.total():
      L = seg.query(0, l + 1)
      R = seg.total() - L
      if L >= R:
          ans += abs(x - X[l])
          seg.update(l, 0)
          x = X[l]
          l -= 1
  
      else:
          ans += abs(x - X[l + 1])
          seg.update(l + 1, 0)
          x = X[l + 1]
          l += 1
  
problem_description: Statement
  
  There are N apartments along a number line, numbered 1 through N. Apartment i
  is located at coordinate X_i. Also, the office of AtCoder Inc. is located at
  coordinate S. Every employee of AtCoder lives in one of the N apartments.
  There are P_i employees who are living in Apartment i.
  
  All employees of AtCoder are now leaving the office all together. Since they
  are tired from work, they would like to get home by the company's bus. AtCoder
  owns only one bus, but it can accommodate all the employees. This bus will
  leave coordinate S with all the employees and move according to the following
  rule:
  
    * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off.
  
    * Repeat the operation above as long as there is one or more employees on the bus.
  
  For a specific example, see Sample Input 1.
  
  The bus takes one seconds to travel a distance of 1. The time required to vote
  and get off the bus is ignorable.
  
  Every employee will vote so that he himself/she herself can get off the bus at
  the earliest possible time. Strictly speaking, when a vote is taking place,
  each employee see which direction results in the earlier arrival at his/her
  apartment, assuming that all the employees follow the same strategy in the
  future. Based on this information, each employee makes the optimal choice, but
  if either direction results in the arrival at the same time, he/she casts a
  vote to the negative direction.
  
  Find the time the bus will take from departure to arrival at the last
  employees' apartment. It can be proved that, given the positions of the
  apartments, the numbers of employees living in each apartment and the initial
  position of the bus, the future movement of the bus is uniquely determined,
  and the process will end in a finite time.
samples: [{"input": "3 2\n    1 3\n    3 4\n    4 2", "output": "4\n    \n\nAssume that the bus moves in the negative direction first. Then, the\ncoordinate of the bus changes from 2 to 1, and the employees living in\nApartment 1 get off. The movement of the bus after that is obvious: it just\ncontinues moving in the positive direction. Thus, if the bus moves in the\nnegative direction first, the coordinate of the bus changes as 2 \u2192 1 \u2192 2 \u2192 3 \u2192\n4 from departure. The time it takes to get home for the employees living in\nApartment 1, 2, 3 are 1, 3, 4 seconds, respectively.\n\nNext, assume that the bus moves in the positive direction first. Then, the\ncoordinate of the bus changes from 2 to 3, and the employees living in\nApartment 2 get off. Afterwards, the bus starts heading to Apartment 1,\nbecause there are more employees living in Apartment 1 than in Apartment 3.\nThen, after arriving at Apartment 1, the bus heads to Apartment 3. Thus, if\nthe bus moves in the positive direction first, the coordinate of the bus\nchanges as 2 \u2192 3 \u2192 2 \u2192 1 \u2192 2 \u2192 3 \u2192 4 from departure. The time it takes to get\nhome for the employees living in Apartment 1, 2, 3 are 3, 1, 6 seconds,\nrespectively.\n\nTherefore, in the beginning, the employees living in Apartment 1 or 3 will try\nto move the bus in the negative direction. On the other hand, the employees\nliving in Apartment 2 will try to move the bus in the positive direction in\nthe beginning. There are a total of 3 + 2 = 5 employees living in Apartment 1\nand 3 combined, which is more than those living in Apartment 2, which is 4.\nThus, the bus will move in the negative direction first, and the coordinate of\nthe bus will change as 2 \u2192 1 \u2192 2 \u2192 3 \u2192 4 from departure.\n\n* * *"}, {"input": "6 4\n    1 10\n    2 1000\n    3 100000\n    5 1000000\n    6 10000\n    7 100", "output": "21\n    \n\nSince the numbers of employees living in different apartments are literally\noff by a digit, the bus consistently head to the apartment where the most\nnumber of employees on the bus lives.\n\n* * *"}, {"input": "15 409904902\n    94198000 15017\n    117995501 7656764\n    275583856 313263626\n    284496300 356635175\n    324233841 607\n    360631781 148\n    472103717 5224\n    497641071 34695\n    522945827 816241\n    554305668 32\n    623788284 22832\n    667409501 124410641\n    876731548 12078\n    904557302 291749534\n    918215789 5", "output": "2397671583"}]