CodeEff/ECCO huggingface dataset:
# Original solution
def dist(a, b):
  res = 0
  for i in range(D):
    res += (a[i] - b[i]) ** 2
  res = res ** 0.5
  return res

N, D = list(map(int, input().split()))
X = [list(map(int, input().split())) for i in range(N)]
ans = 0
for i in range(N - 1):
  for j in range(i +1, N):
    if dist(X[i], X[j]).is_integer():
      ans += 1
print(ans)      

# Optimized solution
import math

N, D = list(map(int, input().split()))

X = [list(map(int, input().split())) for _ in range(N)]

ans = 0
for i in range(N-1):
    for j in range(i+1, N):
        tmp = 0
        for k in range(D): 
            tmp +=(X[i][k]-X[j][k])**2
        tmp = math.sqrt(tmp)
        if (tmp.is_integer()): ans += 1
print(ans)
# Original solution
#!/usr/bin/env python3
import sys
import math


def solve(N: int, D: int, X: "List[List[int]]"):
    count = 0
    for i in range(N):
        for j in range(i + 1,N):
            sum = 0
            for k in range(D):
                sum += (X[i][k] - X[j][k]) ** 2
            f = float(math.sqrt(sum))
            if f.is_integer():
                count += 1
    print(count)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    D = int(next(tokens))  # type: int
    X = [ [ int(next(tokens)) for _ in range(D) ] for _ in range(N) ]  # type: "List[List[int]]"
    solve(N, D, X)

if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys
import math

def solve(N: int, D: int, X: "List[List[int]]"):
    count = 0
    for i in range(0, N - 1):
        for j in range(i + 1, N):
            sum = 0
            for k in range(0, D):
                sum += (X[i][k] - X[j][k]) ** 2
            if math.sqrt(sum).is_integer():
                count += 1
    print(count)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    D = int(next(tokens))  # type: int
    X = [[int(next(tokens)) for _ in range(D)] for _ in range(N)]  # type: "List[List[int]]"
    solve(N, D, X)

if __name__ == '__main__':
    main()
# Original solution
import math

n,d = list(map(int,input().split()))
x = [[0]*d for _ in range(n)]

for i in range(n):
    x[i] = list( map( int, input().split() ) )

count = 0
for i in range(n):
    for j in range(n):
        if i <= j:
            continue
        dist = 0
        for k in range(d):
            dist += (x[i][k] - x[j][k])**2
        dist = math.sqrt(dist)
        if dist % 1 == 0:
            count += 1

print(count)


# Optimized solution
import math

n,d = list(map( int, input().split()))

point = [[0]*d for _ in range(n)]

for i in range(n):
    point[i] = list( map( int, input().split()))

count = 0
for i in range(n):
    for j in range(n):
        if i < j:
            sum = 0
            for k in range(d):
                sum += ( point[i][k] - point[j][k] ) ** 2
            sum = math.sqrt(sum)
            if sum % 1 == 0:
                count += 1
        else:
            continue

print(count)
# Original solution
n,d = list(map(int,input().split()))
x = [list(map(int,input().split())) for i in range(n)]
ans = 0
def k(a,b):
    dist = 0
    for i in range(d):
        dist +=(a[i]-b[i])**2
    dist = dist**(1/2)
    while dist > 0:
        dist -= 1
        if dist == 0:
            return True
    return False
for i in range(n):
    for j in range(i+1,n):
        if k(x[i],x[j]):
            ans += 1

print(ans)

# Optimized solution
n,d = list(map(int,input().split()))
data = [list(map(int,input().split())) for i in range(n)]

def ok(x,y):

    z = 0
    for i in range(d):
        z += (x[i]-y[i])**2
    if z**(0.5) == int(z**(0.5)):
        return True
    else:
        return False

ans = 0
for i in range(n):
    for j in range(i+1,n):
        if ok(data[i],data[j]):
            ans += 1

print(ans)
# Original solution

def main():
  x = int(eval(input()))

  ans = 0

  while x >= 500:
    x -= 500
    ans += 1000

  while x >= 5:
    x -= 5
    ans += 5

  print(ans)

main()


# Optimized solution

def main():
  x = int(eval(input()))

  ans = 0

  ans += (x // 500) * 500
  x -= ans
  ans *= 2

  ans += (x // 5) * 5

  print(ans)

main()
# Original solution
n=int(eval(input()))
ans=0
ans+=(n//500)*1000
n=n%500
ans+=(n//5)*5
print(ans)

# Optimized solution
n=int(eval(input()))
print(((n//500)*1000 + ((n%500)//5)*5))
# Original solution
'''
問題：
    高橋君は金色の硬貨が好きです。
    自分が持っている 500円硬貨 1枚につき 1000、
    5円硬貨 1枚につき 5の嬉しさ を得ます。

    高橋君は X 円を持っています。
    これを高橋君の嬉しさが最大になるように両替したとき、高橋君の嬉しさはいくらになりますか？

    (なお、利用できる硬貨は 500 円玉、100 円玉、50 円玉、10 円玉、5円玉、1 円玉の 6 種類とします。)
'''

'''
    Xは整数
    0 ≦ X ≦ 1,000,000,000
'''
class A:
    def __init__(self, money, yorokobi):
        self.money = money
        self.yorokobi = yorokobi

    def calc_yorokobi(self, credit):    # credit ：残金を入力として受け取り、喜びを計算して返す
        return int(credit / self.money) * self.yorokobi

    # def print_self(self):
        # print(calc_yorokobi(self, credit))
# 
# def calc_yorokobi(input_money, x, yorokobi):
#     ret = input_money // x * yorokobi
#     return ret

# 標準入力から X の値を取得する
input_x = int(eval(input()))

yorokobi_500 = A(500, 1000)
yorokobi_5 = A(5, 5)

result = 0  # 結果格納用
if input_x >= 500:
    # print("1回目：{}".format(result))
    result += yorokobi_500.calc_yorokobi(input_x)   # 高橋500円の喜び
    # print("2回目：{}".format(result))
    result += yorokobi_5.calc_yorokobi(input_x % 500)   # 高橋5円の喜び
    # print("3回目：{}".format(result))
else:
    result += yorokobi_5.calc_yorokobi(input_x)

print(result)

# ret1 = input_x // 500  # 500円で割った商
# ret2 = ret1 * 1000  # 高橋君 500円の喜び
# ret3 = input_x - (ret1 * 500)   # X円から500円の枚数分を引いたお金
# ret4 = ret3 // 5 # （X円から500円の枚数分を引いたお金）÷ 5
# ret5 = ret4 * 5 # 高橋君 5円の喜び
# print(ret2 + ret5)


# Optimized solution
'''
問題：
    高橋君は金色の硬貨が好きです。
    自分が持っている 500円硬貨 1枚につき 1000、
    5円硬貨 1枚につき 5の嬉しさ を得ます。

    高橋君は X 円を持っています。
    これを高橋君の嬉しさが最大になるように両替したとき、高橋君の嬉しさはいくらになりますか？

    (なお、利用できる硬貨は 500 円玉、100 円玉、50 円玉、10 円玉、5円玉、1 円玉の 6 種類とします。)
'''

'''
    Xは整数
    0 ≦ X ≦ 1,000,000,000
'''
class Yorokobi:
    def __init__(self, money, yorokobi):
        self.money = money
        self.yorokobi = yorokobi

    def calc_yorokobi(self, credit):    # credit （残金？）を入力として受け取り、喜びを計算して返す
        return (credit // self.money) * self.yorokobi

# 標準入力から X の値を取得する
input_x = int(eval(input()))

yorokobi_500 = Yorokobi(500, 1000)
yorokobi_5 = Yorokobi(5, 5)

result = 0  # 結果格納用
if input_x >= 500:
    result += yorokobi_500.calc_yorokobi(input_x)   # 高橋君 500円の喜び
    result += yorokobi_5.calc_yorokobi(input_x % 500)   # 高橋君 5円の喜び
else:
    result += yorokobi_5.calc_yorokobi(input_x)

print(result)

# ret1 = input_x // 500  # 500円で割った商
# ret2 = ret1 * 1000  # 高橋君 500円の喜び
# ret3 = input_x - (ret1 * 500)   # X円から500円の枚数分を引いたお金
# ret4 = ret3 // 5 # （X円から500円の枚数分を引いたお金）÷ 5
# ret5 = ret4 * 5 # 高橋君 5円の喜び
# print(ret2 + ret5)
# Original solution
money=int(eval(input()))
happines=0
while money>=500:
  happines+=1000
  money-=500
while money>=5:
  happines+=5
  money-=5
print(happines)

# Optimized solution
money=int(eval(input()))
happines=0
while money>=500:
  happines+=1000
  money-=500
while money>=100:
  happines+=100
  money-=100
while money>=5:
  happines+=5
  money-=5
print(happines)
# Original solution
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし

X = I()
X -= X % 5
X //= 5
print((1000*(X//100) + 5*(X % 100)))


# Optimized solution
# ABC160-B

import sys
def I(): return int(sys.stdin.readline().rstrip())

X = I()
print((1000*(X//500) + 5*((X % 500)//5)))
# Original solution
n=int(eval(input()))
a=0
while n>499:
  n-=500
  a+=1000
while n>4:
  n-=5
  a+=5
print(a)

# Optimized solution
n=int(eval(input()));print((n//500*1000+n%500//5*5))
# Original solution
X = int(eval(input()))
ans = X // 500 * 1000 + (X % 500) // 5 * 5
print(ans)

# Optimized solution
X = int(eval(input()))
print(((X // 500) * 1000 + (X % 500) // 5 * 5))
# Original solution
X = int(eval(input()))
a = X // 500
b = (X % 500) // 5

print((a * 1000 + b * 5))


# Optimized solution
X = int(eval(input()))
ans = (X // 500) * 1000
X = X % 500

ans += (X // 5) * 5

print(ans)
# Original solution
n=int(eval(input()))

k = 0
l = 0
while n>=500:
  n = n-500
  k = k+1

  
while n>5:
  n -= 5 #n=n-5
  l += 1
  

print((k*1000+ l*5  ))

# Optimized solution
n=int(eval(input()))

k = n//500
l = (n-500*k)//5

print((k*1000+ l*5  ))
# Original solution
X = int(eval(input()))

a = X // 500
X = X % 500
b = X // 5

print((a*1000+b*5))

# Optimized solution
X = int(eval(input()))
 
a = X // 500 # 500円硬貨の枚数
X = X % 500 # 端数
b = X // 5 # 5円硬貨の枚数
 
print((a*1000+b*5))
# Original solution
x = int(eval(input()))
ans = (x//500)*1000+((x%500)//5)*5
print(ans)

# Optimized solution
x = int(eval(input()))
print((x//500*1000+(x%500)//5*5))
# Original solution
x = int(eval(input()))
count = 0
count_2 = 0
while x > 0:
    if (x >= 500):
        x -= 500
        count += 1
    else:
        break

while x > 0:
    if (x >= 5):
        x -= 5
        count_2 += 1
    else:
        break

print((1000 * count + 5 * count_2))

# Optimized solution
X = int(eval(input()))

res = X // 500 * 1000
X %= 500
res += X // 5 * 5

print(res)
# Original solution
x = int(eval(input()))
print((1000*(x // 500) + 5*((x % 500) // 5) ))

# Optimized solution
x = int(eval(input()))
print((1000*(x // 500) + 5*(x%500 // 5) ))
# Original solution
x=int(eval(input()))
a=x//500
x=x%500
b=x//5
print((a*1000+b*5))

# Optimized solution
x=int(eval(input()))
a=x%500
b=x//500
print((1000*b+5*(a//5)))
# Original solution
def resolve():
    x=int(eval(input()))
    ans=0
    while x>=500:
        x-=500
        ans+=1000
    while x>=5:
        x-=5
        ans+=5
    print(ans)
resolve()

# Optimized solution
n=int(eval(input()))
gg=0
gg+=(n//500)*1000
n=n-(n//500)*500
gg+=(n//5)*5
print(gg)
# Original solution
X = int(eval(input()))

gohyaku = X//500
nokori = X % 500
goen = nokori//5

print((gohyaku*1000 + goen*5))


# Optimized solution
# Problem B - Golden Coins

X = int(eval(input()))

gohyaku = X // 500
nokori = X % 500
goen = nokori // 5

# output
print((gohyaku * 1000 + goen * 5))
# Original solution
x = int(eval(input()))

ans = 0
ans += (x//500)*1000
x %= 500
ans += (x//5)*5

print(ans)

# Optimized solution
X = int(eval(input()))

num1000 = X // 500
r500 = X % 500
num5 = r500 // 5

print((num1000 * 1000 + num5 * 5))
# Original solution
n=int(eval(input()))

x=n//500

y=(n-x*500)//5

print((x*1000+y*5))

# Optimized solution
n = int(eval(input()))

shou500 = n//500

shou5 = (n-shou500*500)//5

print((shou500*1000+shou5*5))
# Original solution
def main():
    X = int(eval(input()))
    # 500円をp個、5円をq個とすると。500p+5q=Xが成り立ち、ansは1000p+5qになる
    pmax = int(X/500)
    ans = 0
    tmp = 0
    for p in range(pmax+1):
        q = int((X-500*p)/5)
        tmp = 1000*p + 5*q
        ans = max(ans,tmp)
    print(ans)

main()


# Optimized solution
def main():
    X = int(eval(input()))
    # 500円をp個、5円をq個とすると。500p+5q=Xが成り立ち、ansは1000p+5qになる
    p = int(X/500)
    q = int((X-p*500)/5)
    ans = 1000*p + 5*q
    print(ans)

main()
# Original solution
#!/usr/bin/env python3

X = int(eval(input()))
ans = 0

while X >= 500:
    X -= 500
    ans += 1000
while X >= 5:
    X -= 5
    ans += 5
print(ans)
    





# Optimized solution
X = int(eval(input()))
cnt = 0
while X >= 500:
    cnt += 1000
    X -= 500
while X >= 5:
    cnt += 5
    X -= 5
print(cnt)
# Original solution
#template
def inputlist(): return [int(j) for j in input().split()]
from collections import Counter
#template
#issueから始める
X = int(eval(input()))
k = X //500
ans = 0
ans += 1000*k
X -= 500*k
l = X //5
ans += 5*l
print(ans)

# Optimized solution
N = int(eval(input()))
d = N // 500
mod = N % 500
d2 = mod // 5
print((d*1000+d2*5))
# Original solution
S = int(eval(input()))
G = 500
GG = 5

g_count = 0
gg_count= 0

while S >= G:
  S = S - G
  g_count += 1
while S >= GG:
  S = S - GG
  gg_count += 1
print((g_count*1000 + gg_count*5))


# Optimized solution
N = int(eval(input()))
NN = N // 500
MM = N % 500
LL = MM // 5
#print(NN, LL)
print((NN * 1000 + LL * 5))
# Original solution
x = int(eval(input()))
count = 0

while (x >= 500):
    count += 1000
    x = x-500
while (x >= 5):
    if x > 5:
        count += 5
        x = x-5
print(count)

# Optimized solution
#別回答
x = int(eval(input()))

print(((x//500*1000) + ((x%500)//5*5)))
# Original solution
def main():
    n = int(eval(input()))
    ans = 1000*(n//500)
    n %= 500
    ans += 5*(n//5)
    print(ans)


main()


# Optimized solution
def main():
    x = int(eval(input()))
    cnt = 0
    cnt += 1000*(x//500)
    x %= 500
    cnt += 5*(x//5)
    print(cnt)


main()
# Original solution
i = int(eval(input()))
big_count = i // 500
small_count = (i - 500 * big_count) // 5

print((big_count * 1000 + small_count * 5))


# Optimized solution
x = int(eval(input()))

big = x // 500
small = (x - big*500) // 5

print((big * 1000 + small * 5))
# Original solution
n = int(eval(input()))

happy = 0
if n // 500 >= 1:
    ha500 = n // 500
    happy = 1000 * ha500
    nokon = n % 500
    if nokon // 5 >= 1:
        ha005 = nokon // 5
        happy = happy + 5 * ha005
else:
    if n // 5 >= 1:
        ha005 = n // 5
        happy = 5 * ha005
print(happy)

# Optimized solution
n = int(eval(input()))

i = n % 500
happy = ((n//500)*1000 + (i//5)*5)

print(happy)
# Original solution
X =eval(input())
X=int(X)
point =0
while X>=5:
    if X>=500:
        X-=500
        point+=1000
    elif 5<=X<500:
        X-=5
        point+=5

print(point)

# Optimized solution
X =int(eval(input()))
a =int(X/500)
b=int((X%500)/5)
print((1000*a+5*b))
# Original solution
x=int(eval(input()))

result=x//500*1000+x%500//5*5
print(result)

# Optimized solution
a=int(eval(input()))

cnt=(a//500)*1000
cnt+=((a%500)//5)*5

print(cnt)
# Original solution
x =int(eval(input()))
total =0
count =0
for i in range(x):
    if 500*i ==x:
        count =i*1000
        break
    elif 500*i > x:
        total =abs(500*(i-1) -x)
        count =(i-1) *1000
        # print(count)
        break
if total ==0:
    print(count)
else:
    for i in range(x):
        if 5*i ==total:
            print("aa")
            count +=i*5
        elif 5*i >total:
            count +=(i-1)*5
            # print(count)
            # total =abs(5*(i-1) -total)
            break
    print(count)

# Optimized solution
x =int(eval(input()))
total =0
count =0
for i in range(x):
    if 500*i ==x:
        count =i*1000
        break
    elif 500*i > x:
        total =abs(500*(i-1) -x)
        count =(i-1) *1000
        # print(count)
        break
if total ==0:
    print(count)
else:
    for i in range(x):
        if 5*i ==total:
            count +=i*5
            break
        elif 5*i >total:
            count +=(i-1)*5
            break
    print(count)
# Original solution
X = int(eval(input()))
a = X // 500
b = (X % 500) // 5
print((a * 1000 + b * 5))

# Optimized solution
X = int(eval(input()))
print((X // 500 * 1000 + (X % 500)//5*5))
# Original solution
X = int(eval(input()))
print(((X//500)*1000 + (X%500)//5*5))

# Optimized solution
X=int(eval(input()))
print((X//500*1000+X%500//5*5))
# Original solution
x = int(eval(input()))

c_500 = 0
c_5 = 0
while True:
    temp = x - 500
    if temp < 0:
        break
    else:
        c_500 += 1
        x = temp

while True:
    temp = x - 5
    if temp < 0:
        break
    else:
        c_5 += 1
        x = temp

print((c_500 * 2 * 500 + c_5 * 5))


# Optimized solution
x = int(eval(input()))

c_500 = x // 500
r_500 = x % 500
c_5 = r_500 // 5

print((c_500 * 1000 + c_5 * 5))
# Original solution
X = int(eval(input()))
n = X//500
m = X%500
n1 = m//5
print((n*1000+n1*5))

# Optimized solution
X = int(eval(input()))
n = X//500
m = (X%500)//5
print((1000*n+5*m))
# Original solution
X = int(eval(input()))
n = X//500
m = (X%500)//5
print((1000*n+5*m))

# Optimized solution
X = int(eval(input()))
n = X//500
ans = 1000*n
m = X%500
k = m//5
ans += 5*k
print(ans)
# Original solution
s = int(eval(input()))
count1=-1
count2=-1
while s>=0:
 s=s-500
 count1=count1 +1
s=s+500
if s==0:
  yasashisa=count1*1000
  print(yasashisa)
else:
   while s >=0:
     s=s-5
     count2=count2 +1
   yasashisa=count1*1000+count2* 5
   print(yasashisa)





# Optimized solution
s=int(eval(input()))
a= s//500
b=(s%500)//5
yasashisa=a*1000+b*5
print(yasashisa)
# Original solution
x = int(eval(input()))
fh = 0
f = 0
fh = x//500
x%=500
f = x//5
print((1000*fh+5*f))

# Optimized solution
x = int(eval(input()))
ret = (x//500 * 1000) + ((x%500)//5)*5
print(ret)
# Original solution
x = int(eval(input()))

y500 = x // 500
y5 = (x % 500) // 5

print((y500 * 1000 + y5*5))

# Optimized solution
x = int(eval(input()))

coin_500 = x // 500
coin_5 = (x-coin_500*500) // 5

print((coin_500 * 1000 + coin_5 * 5))
# Original solution
X = int(eval(input()))
ans = X//500*1000
X = X % 500
ans += X//5*5
print(ans)


# Optimized solution
def LI():
    return list(map(int, input().split()))


X = int(eval(input()))
ans = X//500*1000
X = X % 500
ans += X//5*5
print(ans)
# Original solution
n = int(eval(input()))
happy = 0
if n%500 == 0:
    print((2*n))
else:
    while n > 0:
        if n == 0:
            break
        n-=500
        if n<0:
            n+=500
            n-=5
            if n<0:
                n+=5
                break
            else:
                happy+=5
        else:
            happy+=1000
    print(happy)


# Optimized solution
x = int(eval(input()))
total = 0
a = int(x / 500)
b = x % 500
c = int(b / 5)
total = a * 1000 + c *5
print(total)
# Original solution
def main():
    n = int(eval(input()))
    print((n//500*1000+(n%500)//5*5))

if __name__ == "__main__":
    main()

# Optimized solution
x = int(eval(input()))
print((x//500*1000+(x%500//5)*5))
# Original solution
x=int(eval(input()))
c=x//500
d=x%500
e=d//5
print((1000*c+e*5))

# Optimized solution
x=int(eval(input()))
a=x//500
b=x%500
c=b//5
print((1000*a+5*c))
# Original solution
x = int(eval(input()))
a = x//500
x -= a*500
b = x//5
ax = a*1000
bx = b*5
print((ax+bx))

# Optimized solution
n = int(eval(input()))
x = n//500
y = n-500*x
z = y//5
print((x*1000+z*5))
# Original solution
s=int(eval(input()))

ans=s//500*1000+(s%500//5)*5
print(ans)

# Optimized solution
x=int(eval(input()))

print(((x//500)*1000+(x%500)//5*5))
# Original solution
from operator import itemgetter

n = int(eval(input()))
lr = [list(map(int, input().split())) + [i] for i in range(n)]

l_sort = sorted(lr, key=itemgetter(0), reverse=True)
r_sort = sorted(lr, key=itemgetter(1))

ans = 0
used = []
now = 0
i = 0
j = 0

if abs(l_sort[0][0]) >= abs(r_sort[0][1]):
	while len(used) != n:
		while l_sort[i][2] in used:
			i += 1
		if not (l_sort[i][0] <= now and now <= l_sort[i][1]):
			ans += abs(now - l_sort[i][0])
			now = l_sort[i][0]
		used.append(l_sort[i][2])
		i += 1
		if len(used) == n:
			break
		while r_sort[j][2] in used:
			j += 1
		if not (r_sort[j][0] <= now and now <= r_sort[j][1]):
			ans += abs(now - r_sort[j][1])
			now = r_sort[j][1]
		used.append(r_sort[j][2])
		j += 1

	print((ans + abs(now)))

else:
	while len(used) != n:
		while r_sort[j][2] in used:
			j += 1
		if not (r_sort[j][0] <= now and now <= r_sort[j][1]):
			ans += abs(now - r_sort[j][1])
			now = r_sort[j][1]
		used.append(r_sort[j][2])
		j += 1
		if len(used) == n:
			break
		while l_sort[i][2] in used:
			i += 1
		if not (l_sort[i][0] <= now and now <= l_sort[i][1]):
			ans += abs(now - l_sort[i][0])
			now = l_sort[i][0]
		used.append(l_sort[i][2])
		i += 1
		#print(now)

	print((ans + abs(now)))

# Optimized solution
from operator import itemgetter

n = int(eval(input()))
lr = [list(map(int, input().split())) for i in range(n)] + [[0, 0]]

l_sort = sorted(lr, key=itemgetter(0), reverse=True)
r_sort = sorted(lr, key=itemgetter(1))

ans = 0
sigma = 0

for i in range(n // 2 + 1):
	sigma += 2 * (l_sort[i][0] - r_sort[i][1])
	if sigma > ans:
		ans = sigma

print(ans)
# Original solution
from heapq import heappush, heappop
N = int(eval(input()))
S = []
for i in range(N):
    l, r = list(map(int, input().split()))
    S.append((l, r))
S.sort()

if all(l <= 0 <= r for l, r in S):
    print((0))
    exit(0)

que = []
P = []
Q = [l for l, r in S]
Q.reverse()
ans = 0
a = b = 0
p = 0
for i in range(N+1):
    if i < N:
        l, r = S[i]
    while que and que[0] <= l:
        P.append(heappop(que))
    cnt = min(len(P), len(Q))
    while p < cnt:
        a += P[p]
        b += Q[p]
        p += 1
    ans = max(ans, abs(a-b)*2)
    if len(P) < len(Q):
        b2 = b + Q[cnt]
        ans = max(ans, abs(a-b2)*2)
    elif len(P) > len(Q):
        a2 = a + P[cnt]
        ans = max(ans, abs(a2-b)*2)
    heappush(que, r)
    if cnt == len(Q) > 0:
        a -= P[cnt-1]
        b -= Q[cnt-1]
    if i < N:
        Q.pop()
print(ans)


# Optimized solution
from heapq import heappush, heappop
N = int(eval(input()))
S = []
for i in range(N):
    l, r = list(map(int, input().split()))
    S.append((l, r))

if all(l <= 0 <= r for l, r in S):
    print((0))
    exit(0)

P = sorted(r for l, r in S)
Q = sorted(l for l, r in S)
L = R = 0
ans = 0
for i in range(N):
    if not i < N-1-i:
        break
    L += P[i]
    R += Q[N-1-i]
    ans = max(ans, abs(R - L)*2)
    if i+1 < N-1-i:
        ans = max(ans, abs(R - L - P[i+1])*2)
    if i < N-1-i-1:
        ans = max(ans, abs(R + Q[N-1-i-1] - L)*2)
print(ans)
# Original solution
N=int(eval(input()))
LR=[None]*N
for i in range(N):
    LR[i]=list(map(int,input().split()))

if len(LR)%2==1:
    LR=LR+[[0,0]]

RAN=(len(LR)+1)//2

LR_L=sorted(LR,key=lambda x: x[0],reverse=True)
LR_R=sorted(LR,key=lambda x: x[1])


ANS=0
for k in range(RAN):
    SUB=0
    for i in range(k+1):
        SUB+=LR_L[i][0]-LR_R[i][1]
        ANS=max(SUB,ANS)

print((ANS*2))


# Optimized solution
N=int(eval(input()))
LR=[None]*N
for i in range(N):
    LR[i]=list(map(int,input().split()))

RAN=(len(LR)+1)//2

LR_L=sorted(LR,key=lambda x: x[0],reverse=True)
LR_R=sorted(LR,key=lambda x: x[1])

ANS_l=[0]*(RAN*2+2)
ANS_r=[0]*(RAN*2+2)

for i in range(RAN):
    ANS_l[2*i+1]=ANS_l[2*i]+LR_L[i][0]
    ANS_l[2*i+2]=ANS_l[2*i+1]-LR_R[i][1]

    ANS_r[2*i+1]=ANS_r[2*i]-LR_R[i][1]
    ANS_r[2*i+2]=ANS_r[2*i+1]+LR_L[i][0]


print((max(max(ANS_l)*2,max(ANS_r)*2)))