Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

N,Q = map(int, input().split())
S = input()

ord_A = ord('A')

P = [[] for _ in range(ord('Z') - ord('A') + 1)]
for i in range(N):
  p = ord(S[i]) - ord_A
  P[p].append(i)

dG = [0 for _ in range(N)]
G = [1 for _ in range(N)]
for i in range(Q):
  t,d = input().split()
  ti = ord(t) - ord_A
  
  for j in P[ti]:
    g = G[j]
    G[j] = 0
    dG[j] = g
  for j in P[ti]:
    dg = dG[j]
    if ('L' == d):
      if (0 < j):
        G[j-1] += dg
    elif ('R' == d):
      if (j < (N-1)):
        G[j+1] += dg
print(sum(G))


### FIXED CODE ###

N,Q = map(int, input().split())
S = input()
C = [tuple(map(str, input().split())) for _ in range(Q)]
C = C[::-1]

left = -1
right = N

for (t,d) in C:
  if ('L' == d):
    if (S[left+1] == t):
      left += 1
      if (N-1 <= left):
        print(0)
        exit()
    if (right < N):
      if (S[right] == t):
        right += 1
        if (N < right):
          right = N
  else:
    if (S[right-1] == t):
      right -= 1
      if (right <= 0):
        print(0)
        exit()
    if (-1 < left):
      if (S[left] == t):
        left -= 1
        if (left < -1):
          left = -1

print(right - left - 1)
### BUGGY CODE ###

l = list(input())
amari = [0]*len(l)
mod = 2019
import math
for i in range(len(l)):
    amari[i] = int(l[i])*(10**(len(l)-1-i)) % mod
rui = [0]*len(l)
rui[len(l)-1] = amari[-1]
for i in range(len(l)-2,-1,-1):
    rui[i] = rui[i+1] + amari[i]
count = [0]*2020
for i in range(len(l)):
    count[rui[i]%2019] += 1
ans = 0
for i in range(1,2020):
    if count[i] == 0 or count[i] == 1:
        continue
    ans += count[i] * (count[i]-1)//2
ans += count[0]
print(ans)

### FIXED CODE ###

S = input()
N = len(S)
P = 2019
num = [0] * P
num[0] = 1
now, ans = 0, 0
_10 = 1
for i in range(N-1, -1, -1):
    now = (now + int(S[i]) * _10) % P
    _10 *= 10
    _10 %= P
    ans += num[now]
    num[now] += 1
print(ans)
### BUGGY CODE ###

import fractions
import copy

n1 = int(input())
nl1 = [int(i1) for i1 in input().split()]

r1 = 0

nl1.sort()
for  i2 in range(n1):
    nl2 = copy.copy(nl1)
    if i2 == 0:
        nl2[i2] = nl1[1]
    else:
        nl2[i2] = nl1[i2 - 1]
    r2 = nl2[0]
    for i3 in range(n1 - 1):
        r2 = fractions.gcd(r2 ,nl2[i3 + 1])
        if r2 <= r1:
            break

    r1 = max(r1,r2)

print(str(r1))

### FIXED CODE ###

import fractions
import sys

n1 = int(input())
nl1 = [int(i1) for i1 in input().split()]

if n1 == 2:
    print(max(nl1))
    sys.exit()

r1 = 0

nl1.sort()

if n1 >= 3:
    n2 = fractions.gcd(nl1[0], nl1[1]) 
    n2 = max(n2, fractions.gcd(nl1[1], nl1[2]))
    n2 = max(n2, fractions.gcd(nl1[0], nl1[2]))

c1 = 0
for i2 in range(n1):
    if nl1[i2] % n2 != 0:
        c1 += 1
    if c1 >= 2:
        break
    if i2 == (n1 - 1):
        r1 = n2

i3 = n2

while r1 == 0:
    c1 = 0
    for i4 in range(n1):
        if nl1[i4] % i3 != 0:
            c1 += 1
        if c1 >= 2:
            break
        if i4 == (n1 - 1):
            r1 = i3
    i3 -= 1

print(str(r1))
### BUGGY CODE ###

#D問題
import copy

N = int(input())
A = [list(map(int,input().split())) for i in range(N)]
G = copy.deepcopy(A)
whole = 0
for i in range(N):
    for j in range(N):
        whole+=A[i][j]


#print(A,G)
#warshall_floyd法
def warshall_floyd(dist):
    global N
    #dist[i][j]: iからjへの最短距離
    for k in range(N):
        for i in range(N):
            for j in range(N):
                dist[i][j] = min(dist[i][j],dist[i][k] + dist[k][j])

warshall_floyd(G)
#print(A,G)

flag = 0
ans = 0
for i in range(N):
    for j in range(N):
        if A[i][j] != G[i][j]:
            flag = 1
        else:
            for k in range(N):
                if k != i and k != j:
                    #print(G[i][j],G[i][k]+G[k][j])
                    if G[i][j] == G[i][k] + G[k][j]:
                        ans+=A[i][j]
                        #print(A[i][j])
                        break
                
            
if flag == 1:
    print(-1)
else:
    print((whole-ans)//2)

### FIXED CODE ###

#D問題
import copy

N = int(input())
A = [list(map(int,input().split())) for i in range(N)]
G = copy.deepcopy(A)
whole = 0
for i in range(N):
    for j in range(N):
        whole+=A[i][j]


#print(A,G)
#warshall_floyd法
def warshall_floyd(dist):
    global N
    #dist[i][j]: iからjへの最短距離
    for k in range(N):
        for i in range(N):
            for j in range(N):
                dist[i][j] = min(dist[i][j],dist[i][k] + dist[k][j])

warshall_floyd(G)
#print(A,G)

flag = 0
ans = 0
for i in range(N):
    for j in range(N):
        if A[i][j] != G[i][j]:
            flag = 1
        else:
            for k in range(N):
                if k != i and k != j:
                    #print(G[i][j],G[i][k]+G[k][j])
                    if G[i][j] == G[i][k] + G[k][j]:
                        ans+=A[i][j]
                        #print(A[i][j])
                        break
                
            
if flag == 1:
    print(-1)
else:
    print((whole-ans)//2)
### BUGGY CODE ###

import math
n=int(input())
lis=[]
ans=[]
for i in range(n):
    lis.append(input())
    ans.append([0,0,0,0,0,0,0,0,0,0])
    for j in range(10):
        ans[i][j]=lis[i][j]
    ans[i].sort()

a=0
for i in range(n-1):
    for j in range(n-1-i):
        if ans[i]==ans[i+j+1]:
            a+=1

print(a)


### FIXED CODE ###

n=int(input())
ans=[]
for i in range(n):
    qw=input()
    ans.append([0,0,0,0,0,0,0,0,0,0])
    for j in range(10):
        ans[i][j]=qw[j]
    ans[i].sort()

lis=[]
for i in range(n):
    lis.append(ans[i][0])
    for j in range(9):
        lis[i]+=ans[i][j+1]

a=0
#print(lis)

dic={lis[0]:1}
for i in range(n-1):
    if lis[i+1] in dic.keys():
        a+=dic[lis[i+1]]
        dic[lis[i+1]]+=1
    else:
        dic[lis[i+1]]=1

print(int(a))
### BUGGY CODE ###

#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A
def A():
    return

#B
def B():
    return

#C
def C():
    s = S()
    k = I()
    for i in range(k):
        if s[i] != "1":
            print(s[i])
            quit()
    print(1)

#D
def D():
    n,m,q = LI()
    v = [[] for i in range(n)]
    for i in range(m):
        l,r = LI()
        v[l-1].append(r-1)
        v[l-1].sort()
    f = [[bisect.bisect_right(v[i],j) for j in range(n)] for i in range(n)]
    for i in range(q):
        p,q = LI()
        p -= 1
        q -= 1
        ans = 0
        for j in range(p,q+1):
            ans += f[j][q]
        print(ans)
#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == "__main__":
    D()


### FIXED CODE ###

#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A
def A():
    return

#B
def B():
    return

#C
def C():
    s = S()
    k = I()
    for i in range(k):
        if s[i] != "1":
            print(s[i])
            quit()
    print(1)

#D
def D():
    n,m,q = LI()
    v = [[] for i in range(n)]
    for i in range(m):
        l,r = LI()
        v[l-1].append(r-1)
    for i in range(n):
        v[i].sort()
    f = [[bisect.bisect_right(v[i],j) for j in range(n)] for i in range(n)]
    for i in range(q):
        p,q = LI()
        p -= 1
        q -= 1
        ans = 0
        for j in range(p,q+1):
            ans += f[j][q]
        print(ans)
#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == "__main__":
    D()
### BUGGY CODE ###

def main():
    s = input()
    t = input()
    sett = list(set((t)))
    for i in sett:
        if i not in s:
            print(-1)
            return
    lens = len(s)
    lent = len(t)
    lensett = len(sett)
    # index[i文字目以降][settのindex]に何文字目にあるかが格納される
    index = [[] for _ in range(lens)]
    for i in range(lens):
        x = s[i:] + s[:i]
        for j in sett:
            index[i].append(x.index(j))
    ans = 0
    n = 0
    for i in t:
        j = sett.index(i)
        ans += index[n % lens][j] + 1
        n += index[n % lens][j] + 1
    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    s = input()
    t = input()
    s2 = s * 2
    sett = list(set((t)))
    settindex = {}
    for i in sett:
        settindex[i] = sett.index(i)
        if i not in s:
            print(-1)
            return
    lens = len(s)
    # index[i文字目以降][settのindex]に何文字目にあるかが格納される
    index = [[-1] * len(sett) for _ in range(lens)]
    ans = 0
    n = 0
    for i in t:
        j = settindex[i]
        if index[n][j] == -1:
            index[n][j] = s2[n:].index(i)
        ans += index[n][j] + 1
        n += index[n][j] + 1
        n %= lens
    print(ans)


if __name__ == '__main__':
    main()
### BUGGY CODE ###

import sys

def input():
    return sys.stdin.readline().strip()

def main():
    N,W = map(int,input().split())
    goods = [list(map(int,input().split())) for _ in range(N)]

    dp = [[0 for _ in range(W+1)] for _ in range(N+1)]

    for i in range(N):
        for w in range(W+1):
            if w >= goods[i][0]:
                dp[i+1][w] = max(dp[i][w-goods[i][0]]+goods[i][1],dp[i][w])
            else:
                dp[i+1][w] = dp[i][w]
    print(dp[N][W])

if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys

def input():
    return sys.stdin.readline().strip()

def main():
    N,W = map(int,input().split())
    goods = [list(map(int,input().split())) for _ in range(N)]

    dp = [[0 for _ in range(W+1)] for _ in range(N+1)]

    for i in range(N):
        for w in range(W+1):
            if w >= goods[i][0]:
                dp[i+1][w] = max(dp[i][w-goods[i][0]]+goods[i][1],dp[i][w])
            else:
                dp[i+1][w] = dp[i][w]
    print(dp[N][W])

if __name__ == "__main__":
    main()
### BUGGY CODE ###

t = input()
le = len(t)
for i in range(le):
    c = t[i]
    if t[i] == '?':
        c = 'D'
        if i > 0 and t[i - 1] == 'P':
            c = 'D'
        elif i == 0 and le > 1 and t[i + 1] == 'D' or i == 0 and le > 1 and t[i + 1] == '?':
            c = 'P'
        elif i < le - 1 and t[i + 1] == 'D' or i < le - 1 and t[i + 1] == '?':
            c = 'P'
    t = ''.join([t[:i], c, t[i + 1:]])
print(t)   
        

### FIXED CODE ###

t = input()
le = len(t)
tt = []
for i in range(le):
    c = t[i]
    if t[i] == '?':
        c = 'D'
        if i > 0 and tt[i - 1] == 'P':
            c = 'D'
        elif i == 0 and le > 1 and t[i + 1] == 'D' or i == 0 and le > 1 and t[i + 1] == '?':
            c = 'P'
        elif i < le - 1 and t[i + 1] == 'D' or i < le - 1 and t[i + 1] == '?':
            c = 'P'
    tt.append(c)
print(''.join(tt))
### BUGGY CODE ###

N = int(input())
arr = [int(input()) for _ in range(N)]

for i in range(N):
    a = arr[:i] + arr[i+1:]
    print(max(a))

### FIXED CODE ###

N = int(input())
arr = [int(input()) for _ in range(N)]
sarr = sorted(arr, reverse=True)

for i in range(N):
    a = arr[i]
    if a == sarr[0]:
        print(sarr[1])
    else:
        print(sarr[0])
### BUGGY CODE ###

N,M=map(int,input().split())
L = []
R = []
for i in range(M):
    x1,y1=[int(i) for i in input().split()]
    L.append(x1)
    R.append(y1)

for i in range(M):
    if i==0:
        landl=set(range(L[i],R[i]+1))
    else:
        landl=landl & set(range(L[i],R[i]+1))


print(len(landl))

### FIXED CODE ###

N,M=map(int,input().split())
L = []
R = []
for i in range(M):
    x1,y1=[int(i) for i in input().split()]
    L.append(x1)
    R.append(y1)

L2=max(L)
R2=min(R)

if L2<=R2:
    print(R2-L2+1)
else:
    print(0)
### BUGGY CODE ###

import math

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

MAX=10**5
limit=int(math.sqrt(MAX))
primes=[2]
table=[i+1 for i in range(2,MAX,2)]
while limit>table[0]:
  primes.append(table[0])
  table=[j for j in table if j%table[0] != 0]
table=primes+table

similars=[]
for i in range(len(table)):
  n=table[i]
  if (n+1)//2 in table:
    similars.append(n)

for i in lr:
  l=i[0]
  r=i[1]
  cand=[x for x in similars if l<=x<=r]
  print(len(cand))

### FIXED CODE ###

import math
from itertools import accumulate

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

MAX=10**5
limit=int(math.sqrt(MAX))
primes=[2]
table=[i+1 for i in range(2,MAX,2)]
while limit>table[0]:
  primes.append(table[0])
  table=[j for j in table if j%table[0] != 0]
primes=set(primes+table)

similars=[0]*MAX
for i in range(2,MAX):
  if i in primes and (i+1)//2 in primes:
    similars[i]=1

similars=list(accumulate(similars))

for i in lr:
  l=i[0]
  r=i[1]
  print(similars[r]-similars[l-1])
### BUGGY CODE ###

d = tuple([1] + [6**i for i in range(1, 7)] + [9**i for i in range(1, 6)])
M = 10**6+1
B = [float("inf")]*M
B[0] = 0
for i in range(M):
  for j in d:
    if i+j < M:
      B[i+j] = min(B[i+j], B[i]+1)
n = int(input())
print(B[n])

### FIXED CODE ###

d = tuple([1] + [6**i for i in range(1, 7)] + [9**i for i in range(1, 6)])
M = 10**6+1
B = [float("inf")]*M
B[0] = 0
for i in range(M):
  for j in d:
    if i+j < M:
      B[i+j] = min(B[i+j], B[i]+1)
n = int(input())
print(B[n])
### BUGGY CODE ###

import sys #追加
sys.setrecursionlimit(10**9)

n,m = map(int,input().split())
al = list(map(int,input().split()))




bl = [i//2 for i in al]
from fractions import gcd
ans = 1
for b in bl:
   ans = (ans * b) // gcd(ans, b)

for a in al:
   if ans % a == 0:
      print(0)
      import sys
      sys.exit()

def ceil(a, b):
   return a // b + (a % b > 0)

print(ceil(m // ans, 2))


### FIXED CODE ###

import sys #追加
sys.setrecursionlimit(10**9)
import time
s = time.time()

n,m = map(int,input().split())
al = list(map(int,input().split()))

bl = [i//2 for i in al]
from fractions import gcd
ans = 1
for b in bl:
   if time.time() - s > 1.9:
      print(0)
      import sys
      sys.exit()
   ans = (ans * b) // gcd(ans, b)

for a in al:
   if time.time() - s > 1.9:
      print(0)
      import sys
      sys.exit()
   if ans % a == 0:
      print(0)
      import sys
      sys.exit()

def ceil(a, b):
   return a // b + (a % b > 0)

print(ceil(m // ans, 2))
### BUGGY CODE ###

N,M=map(int,input().split())
ab=[list(map(int,input().split())) for l in range(N)]
ct=0
money=0
l=[]
for i in range(N):
    l.append(ab[i][0])
while True:
    min_id=l.index(min(l))
    for i in range(ab[min_id][1]):
        money+=l[min_id]
        ct+=1
        if ct==M:
            break
    ab[min_id][0]=100000000000
    l[min_id]=100000000000
    if ct==M:
        break
print(money)


### FIXED CODE ###

N,M=map(int,input().split())
ab=[list(map(int,input().split())) for l in range(N)]
ct=0
ct1=0
ct2=0
money=0
l=[]
id=[]
for i in range(N):
    l.append(ab[i][0])
id=sorted(range(len(l)),key=lambda k: l[k])
l.sort()
while ct<M:
    while ct2<ab[id[ct1]][1]:##
        money+=l[ct1] #l[ct1]
        #print(money,l[ct1])
        ct+=1
        ct2+=1
        if ct==M:
            break
    ct1+=1
    ct2=0
print(money)
### BUGGY CODE ###

#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]


#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

import heapq

#flag=Trueは大きい方
def index(n,flag=True):
    if flag==True:
        if n==N-1:
            return 0
        else:
            return n+1
    else:
        if n==0:
            return N-1
        else:
            return n-1

N = I()
A = III()
B = III()

x = []
for i in range(N):
    heapq.heappush(x,[-B[i],i])

counter = 0

while x:
    i = x[0][1]
    b = -x[0][0]
    a = B[index(i,False)]
    c = B[index(i)]
    if b == A[i]:
        heapq.heappop(x)
        continue
    if b<A[i]:
        print(-1)
        exit()
    else:
        if b<=a+c:
            print(-1)
            exit()
        temp = (b-A[i])/(a+c)
        if temp == (b-A[i])//(a+c):
            heapq.heappop(x)
            B[i] = A[i]
            counter += int(temp)
        else:
            count = b//(a+c)
            b -= count*(a+c)
            heapq.heappop(x)
            heapq.heappush(x,[-b,i])
            B[i] = b
            counter += count

print(counter)

### FIXED CODE ###

#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]


#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

import heapq

N = I()
A = III()
B = III()

x = []
for i,b in enumerate(B):
    heapq.heappush(x,(-b,i))

counter = 0

while x:
    temp = heapq.heappop(x)
    i = temp[1]
    b = -temp[0]
    a = B[(i-1)%N]
    c = B[(i+1)%N]
    if b == A[i]:
        continue
    elif b<A[i]:
        print(-1)
        exit()
    else:
        d,m = divmod(b-A[i],a+c)
        if d==0:
            print(-1)
            exit()
        elif m == 0:
            B[i] = A[i]
            counter = counter+d
        else:
            B[i] = A[i]+m
            heapq.heappush(x,(-B[i],i))
            counter = counter+d

print(counter)
### BUGGY CODE ###

import collections

n = int(input())
a = list(map(int,input().split()))
s = collections.deque()

for i in a:
    if i not in s:
        s.appendleft(i)
    else:
        print("NO")
        exit()

print("YES")

### FIXED CODE ###

import collections

n = int(input())
a = list(map(int,input().split()))
a.sort()

for i in range(n-1):
    if a[i] == a[i+1]:
        print("NO")
        exit()

print("YES")
### BUGGY CODE ###

N = int(input())
S = input()

total = 0

for i in range(N - 2):
  for  j in range(i + 1, N - 1):
    if S[i] == S[j]:
      continue
    for k in range(j + 1, N):
      if (j - i) == (k - j):
        continue
      if (S[i] == S[k]) | (S[j] == S[k]):
        continue
      total += 1
      
print(total)

### FIXED CODE ###

N = int(input())
S = input()

RGB = S.count("R") * S.count("G") * S.count("B")

for i in range(N - 2):
  for j in range(i + 1, N - 1):
    k = 2 * j - i
    if ( k >= N ):
      break
    if (S[i] != S[j]) & (S[j] != S[k]) & (S[i] != S[k]):
      RGB -= 1


print(RGB)
### BUGGY CODE ###

N = int(input())
A = [int(input()) for i in range(N)]

lenA = len(A)
cnt = 0
chk = [0]*lenA

for i in range(lenA):
    if chk[i]:
        continue
    chk[i] = 1

    last = A[i]
    for j, v in enumerate(A[i+1:]):
        if chk[i+j+1] or v<=last:
            continue
        else:
            chk[i+j+1] = 1
            last = v
    cnt += 1

print(cnt)

### FIXED CODE ###

from collections import deque
from bisect import bisect_left as bl
N = int(input())
A = [int(input()) for i in range(N)]

de = deque([])
de.append(A[0])
for a in A[1:]:
    ind = bl(de, a)
    if ind == 0:
        de.appendleft(a)
    else:
        de[ind-1] = a

print(len(de))
### BUGGY CODE ###

# from collections import defaultdict,deque
# import sys,heapq,bisect,math,itertools,string,queue,copy,time
# sys.setrecursionlimit(10**8)
#
#
import sys
import itertools
INF = float("INF")

N = int(input())
AA = list(map(int, sys.stdin.readline().split()))
BB = list(map(int, sys.stdin.readline().split()))

cards = set()
for i,(a,b) in enumerate(zip(AA,BB)):
    if i%2 == 0:
        cards.add((a, b, i))
    else:
        cards.add((b, a, i))

def solve(ii):
    tmp = 0
    for _ in range(N):
        next = []
        for k, i in enumerate(ii):
            if i == 0:
                tmp += k
            else:
                next.append(i-1)
        ii = next[:]
    return tmp

ans = INF
for Acards in itertools.combinations(cards, (N+1)//2):
    Bcards = cards - set(Acards)

    Ais = sorted([(card[0], card[2]) for card in Acards])
    Bis = sorted([(card[1], card[2]) for card in Bcards])

    bn = -1
    ii = []
    flag = False
    for i in range(N):
        if i%2 == 0:
            n,i = Ais[i//2]
        else:
            n,i = Bis[i//2]
        if bn > n:
            flag = True
        bn = n
        ii.append(i)

    if flag: continue

    ans = min(ans,solve(ii))


if ans == INF:
    print(-1)
else:
    print(ans)


### FIXED CODE ###

from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())


N = inp()
AA = inpl()
BB = inpl()

# In[]:
# N = 3
# AA = [3, 4, 3]
# BB = [3, 2, 3]

cards = []
for i,(a,b) in enumerate(zip(AA,BB)):
    if i%2 == 0:
        cards.append((a, b, i))
    else:
        cards.append((b, a, i))


n = (N+1)//2

# In[]:
def solve(ii):
    tmp = 0
    # print(ii)
    for _ in range(N):
        next = []
        for k, i in enumerate(ii):
            if i == 0:
                tmp += k
            else:
                next.append(i-1)
        ii = next[:]
    # print(tmp)
    return tmp



# In[]:
ans = INF
for Acards in itertools.combinations(cards, n):
    Ais = sorted([(Acard[0], Acard[2]) for Acard in Acards])

    ii = set([i for A,i in Ais])
    Bis = []
    for a,b,i in cards:
        if i not in ii:
            Bis.append((b,i))
    Bis.sort()

    nums = []
    ii = []
    for i in range(N):
        if i%2 == 0:
            n,i = Ais[i//2]
        else:
            n,i = Bis[i//2]
        nums.append(n)
        ii.append(i)

    # print(nums,ii)
    if nums != sorted(nums):
        continue
    else:
        ans = min(ans,solve(ii))


if ans == INF:
    print(-1)
else:
    print(ans)