#include <bits/stdc++.h>
#define pb push_back
#define SZ(x) ((int)(x.size()))
#define FOR(i,s,n) for (ll i = (s); (i) < (n); ++i)
#define FORD(i,s,l) for (ll i = (s); (i) >= l; --i)
#define F first
#define S second
#define TC int __tc; cin >> __tc; FOR(case_num,1,__tc+1)
#define TEST(x,i) ((x)&(1ll<<(i)))
#define SET(x,i) ((x)|(1ll<<(i)))
#define FLIP(x,i) ((x)^(1ll<<(i)))
#define CLEAR(x,i) ((x)&~(1ll<<(i)))
const double pi = 4 * atan(1);
using namespace std;
typedef long long ll;

template<class ForwardIterator> void print_vec(ForwardIterator first, ForwardIterator last, string sep = " ", string end="\n") {
	bool ft = true;
	while (first != last) {
		if (! ft) {cout << sep;} else {ft = false;}
		cout << (*first);
		++first;
	}
	cout << end;
}

inline ll floorDiv(ll x, ll y) {
	ll d = x / y;
	ll r = x % y;
	return r ? (d - ((x < 0) ^ (y < 0))) : d;
}

ll ceilDiv(ll x, ll y) {
	return -floorDiv(-x, y);
}

bool predicate(ll mid) {
	return mid > 10;
}

ll bin_search(ll lo, ll hi) {
	int sign = lo < hi ? 1 : -1;
	lo *= sign;
	hi *= sign;
	hi++;
	while (lo < hi) {
		ll mid = floorDiv(lo + hi, 2);
		if (!predicate(sign * mid)) {
			lo = mid + 1;
		} else {
			hi = mid;
		}
	}
	return sign * lo;
}
const ll MOD = 1000000007;
const int MAXN = 200005;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	set<ll> s;
	int n;
	cin >> n;
	for(int i = 0; i < n; i++) {
		ll x;
		cin >> x;
		s.insert(x);
	}
	
	while(s.size() > 1) {
		ll x = *s.begin();
		ll X = *prev(s.end());
		s.erase(X);
		s.insert(X-x);
	}
	
	cout << (*s.begin()) << "\n";

}

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) 
#define X first
#define Y second 
#define nl '\n'
#define AC return 0
#define pb(a) push_back(a) 
#define mst(a,b) memset(a, b, sizeof a)
#define rep(i,n) for(int i = 0; (i)<(n); i++)
#define rep1(i,n) for(int i = 1; (i)<=(n); i++)
#define scd(a) scanf("%lld", &a)
#define scdd(a,b) scanf("%lld%lld", &a, &b)
#define scs(s) scanf("%s", s)
//#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const ll INF = (ll)0x3f3f3f3f3f3f3f, MAX = 9e18, MIN = -9e18;
const int N = 1e6+10, M = 2e6+10, mod = 1e9+7, inf = 0x3f3f3f3f;
ll a[N], b[N], c[N], s[N], cnt[N]; 
vector<int> pos[N];
map<int, int> mp;

void add(int i, int n)
{
	while(i <= n)
		c[i]++, i += i&-i;
}

int get(int i)
{
	int s = 0;
	while(i)
		s += c[i], i -= i&-i;
	return s;
}

int main()
{
	IOS;
	int _;
	// cin>>_;
	_ = 1;
	while(_--)
	{
		ll n, id = 0, ans = 0;
		cin>>n;
		rep1(i, n)	cin>>a[i], a[i] += i;
		rep1(i, n)	cin>>b[i], b[i] += i;
		// rep1(i, n)	cout<<a[i]<<' ';cout<<nl;
		// rep1(i, n)	cout<<b[i]<<' ';cout<<nl;
		rep1(i, n)
		{
			if(!mp[a[i]])	mp[a[i]] = ++id;
			pos[mp[a[i]]].pb(i);
		}
		rep1(i, n)
		{
			if(cnt[mp[b[i]]] >= pos[mp[b[i]]].size())
			{
				cout<<-1<<nl;
				AC;
			}
			ans += get(n) - get(pos[mp[b[i]]][cnt[mp[b[i]]]]);
			add(pos[mp[b[i]]][cnt[mp[b[i]]]++], n);
		}
		cout<<ans<<nl;
	}
	
	
	AC;
} 
