#include <iostream>

using namespace std;

int64_t gcd(int64_t a, int64_t b)
{
   if (a%b == 0)
   {
       return(b);
   }
   else
   {
       return(gcd(b, a%b));
   }
}

int64_t lcm(int64_t a, int64_t b)
{
   return a * b / gcd(a, b);
}

int main()
{
   int N;
   cin >> N;
   int64_t arrlcm = 1;
   for(int i = 2; i <= N; i++){
       arrlcm = lcm(i,arrlcm);
   }
   cout << arrlcm + 1 << endl;
   return 0;
}
//#include <bits/stdc++.h>
//using namespace std;
#include <set>
#include <cstdio>
#include <iostream>
using namespace std;

#define rep(i,_l,_r) for(signed i=(_l),_end=(_r);i<=_end;++i)
#define fep(i,_l,_r) for(signed i=(_l),_end=(_r);i>=_end;--i)
#define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
#define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
#define print(x,y) write(x),putchar(y) 
//#define debug(...) do {cerr<<__LINE__<<" : ("#__VA_ARGS__<<") = "; Out(__VA_ARGS__); cerr<<flush;} while(0)
//template <typename T> void Out(T x) {cerr<<x<<"\n";}
//template <typename T,typename ...I> void Out(T x,I ...NEXT) {cerr<<x<<", "; Out(NEXT...);}

template <class T> inline T read(const T sample) {
    T x=0; int f=1; char s;
    while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
    while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
    return x*f;
}
template <class T> inline void write(const T x) {
    if(x<0) return (void) (putchar('-'),write(-x));
    if(x>9) write(x/10);
    putchar(x%10^48);
}
template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
template <class T> inline T fab(const T x) {return x>0?x:-x;}
template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}

typedef pair <int,int> pii; 

const int maxn=2e5+5;

set <pii> s;
int n;
long long c[maxn];
set <pii> :: iterator it;

int lowbit(int x) {
	return x&-x;
}

void add(int x,int k) {
	while(x<=n) c[x]+=k,x+=lowbit(x);
} 

long long ask(int x) {
	long long r=0;
	while(x) r+=c[x],x-=lowbit(x);
	return r;
}

int main() {
	n=read(9); long long ans=0;
	rep(i,1,n) s.insert(make_pair(read(9)+i,i));
	rep(i,1,n) {
		int x=read(9); it=s.lower_bound(make_pair(x+i,0));
		if(it==s.end() || (it->first)!=x+i) return puts("-1"),0;
		long long tmp=ask(it->second);
		add(1,1),add(it->second,-1);
		ans+=tmp+(it->second)-i;
		s.erase(it);
	}
	print(ans,'\n');
	return 0;
}
