n=int(input()) a=list(map(int,input().split())) l,r=0,2*n-1 ans=0 while la[r]-a[r-1]: ans+=a[l]-a[l+1] l+=2 else: ans+=a[r]-a[r-1] r-=2 print(ans)