Problem:
Leapfrog
Problem Statement
N
個のマスが円状に並んでいる。マスは時計回りに
1,\ 2,\
...
,\ N
と番号が振られている。各
i
(
1 ≤  i ≤  N−1
) について、
i
番目のマスと
i+1
番目のマスは隣り合う。また、
N
番目のマスと
1
番目のマスは隣り合う。
これらのうち
M
個のマスには、互いに区別できない駒が
1
個ずつ置かれている。はじめ、
x_1,\ x_2,\
...
,\ x_M
番目のマスに駒が置かれている。次の操作を何回か行い、
y_1,\ y_2,\
...
,\ y_M
番目のマスに駒が置かれているようにしたい。
時計回りまたは反時計回りに連続する
3
個のマスを選び、順に
A,\ B,\ C
とおく。
A
と
B
にそれぞれ駒があり
C
に駒がないならば、
A
の駒を
C
へ移動する。
y_1,\ y_2,\
...
,\ y_M
番目のマスに駒が置かれているようにできるか判定せよ。できるならば、必要な操作の回数の最小値を求めよ。
Constraints
3 ≤  N ≤  3,000
1 ≤  M ≤  N
1 ≤  x_1<x_2<
...
<x_M ≤  N
1 ≤  y_1<y_2<
...
<y_M ≤  N
Input Format
入力は以下の形式で標準入力から与えられる。
N
M
x_1
x_2
...
x_M
y_1
y_2
...
y_M
Output Format
y_1,\ y_2,\
...
,\ y_M
番目のマスに駒が置かれているようにできるならば、必要な操作の回数の最小値を一行に出力せよ。できないならば、代わりに
-1
を一行に出力せよ。
Sample Input 1
7 2
1 2
5 6
Sample Output 1
3
次のように
3
回の操作を行えばよい。
2
番目のマスの駒を
7
番目のマスへ移動する。
1
番目のマスの駒を
6
番目のマスへ移動する。
7
番目のマスの駒を
5
番目のマスへ移動する。
Sample Input 2
3 1
1
2
Sample Output 2
-1
Sample Input 3
2999 3
1 2 3
2 3 4
Sample Output 3
4491004

Submitted solutions:

code_snippet: #include <string>
#include <vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<functional>
#include<list>
#include<deque>
#include<bitset>
#include<set>
#include<map>
#include<unordered_map>
#include<cstring>
#include<sstream>
#include<complex>
#include<iomanip>
#include<numeric>
#define X first
#define Y second
#define pb push_back
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define repe(X,Y) for ((X) = 0;(X) < (Y);++(X))
#define peat(X,Y) for (;(X) < (Y);++(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
 
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<class T> using vv=vector<vector<T>>;
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"{"; rep(i,t.size()) {os<<t[i]<<",";} os<<"}"<<endl; return os;}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
const ll MOD=1e9+7;
const ll INF=1e16;
 
bool movable(vector<int> &v){
  int n=v.size();
  rep(i,n)
    if( !v[(i+1)%n] && v[i]+v[(i+2)%n]==1 ) return 1;
  return 0;
}
 
void solve(pii a,pii b,int n){
  cout<<a<<b<<n<<endl;
  int x,y;
  ll re=INF;
  x=abs(a.X-b.X); y=abs(a.Y-b.Y);
  if(x%2==y%2){
    cout<<"<<"<<endl;
    if(x%2){ x=n-x; y=n-y;}
    re=min<ll>(re,(x+y)/2);
  }
  x=abs(a.X-b.Y); y=abs(a.Y-b.X);
  if(x%2==0 && y%2==1){
    cout<<"<>"<<endl;
    x=n-x;
    re=min<ll>(re,(x+y)/2);
  }
  cout<<(re==INF?-1:re)<<endl;
}
 
int main(){
  ios_base::sync_with_stdio(false);
  cout<<fixed<<setprecision(0);
  int n,m,x;
  cin>>n>>m;
  vector<int> xs(n,1),ys(n,1);
  rep(i,m){
    cin>>x; --x;
    xs[x]=0;
  }
  rep(i,m){
    cin>>x; --x;
    ys[x]=0;
  }
  if(!movable(xs)){
    cout<<(xs==ys?0:-1)<<endl;
    return 0;
  }
  vector<int> a,b;
  rep(j,3)
    rep(i,n) if(xs[i]) a.pb(i);
  rep(j,3)
    rep(i,n) if(ys[i]) b.pb(i);
  ll ans=INF;
  //cout<<a<<b;
  m=n-m;
  //cout<<m<<endl;
  rep(t,4)rep(i,m+1){
    int f=1;
    ll re=0;
    rep(j,m){
      int d=a[i+j]-b[j]+(t-2)*n;
      if(i+j>=m) d=n+d;
      d=abs(d);
      //cout<<d<<",";
      if(d%2){
	f=0; break;
      }else{
	re+=d/2;
      }
    }
    //cout<<i<<";"<<1<<endl;
    if(f)ans=min(re,ans);
  }
  /*rep(i,m+1){
      int f=1;
      ll re=0;
      rep(j,m){
	int d=abs(a[i+j]-b[j]);
	if(i+j>=m) d=n-d;
	cout<<d<<",";
	if(d%2){
	  f=0; break;
	}else{
	  re+=d/2;
	}
      }
      cout<<i<<";"<<1<<endl;
      if(f)ans=min(re,ans);
    }
    rep(i,m+1){
      int f=1;
      ll re=0;
      rep(j,m){
	int d=abs(a[i+j]-b[j]);
	if(i+j>=m) d=n+n-d;
	else d=n-d;
	cout<<d<<",";
	if(d%2){
	  f=0; break;
	}else{
	  re+=d/2;
	}
      }
      cout<<i<<":"<<2<<endl;
      if(f)ans=min(re,ans);
    }
    /*rep(i,m+1){
      int f=1;
      ll re=0;
      rep(j,m){
	int d=abs(a[j]-b[i+j]);
	if(i+j>=m) d=n-d;
	cout<<d<<",";
	if(i==1)cout<<d<<",";
	if(d%2){
	  f=0; break;
	}else{
	  re+=d/2;
	}
      }
      if(f)ans=min(re,ans);
      if(re==1501&&f)cout<<i<<":"<<3<<endl;
    }
    rep(i,m+1){
      int f=1;
      ll re=0;
      rep(j,m){
	int d=abs(a[j]-b[i+j]);
	if(i+j>=m) d=n+d;
	cout<<d<<",";
	if(d%2){
	  f=0; break;
	}else{
	  re+=d/2;
	}
      }
      if(f)ans=min(re,ans);
      if(re==1501&&f)cout<<i<<":"<<4<<endl;
    }*/
  cout<<(ans==INF?-1:ans)<<endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3588
final_rank: 1

code_snippet: // AOJ #2716 Leapfrog
// 2025.3.25

#include <bits/stdc++.h>
using namespace std;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {
	int n = 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(int n) {
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

const int NMAX = 3005;
const int INF = 1001001001;

int a[NMAX], b[NMAX];

int main(){
    int n = Cin(), m = Cin();
    if(n == m){
        Cout(0);
        return 0;
    }

    for (int i = 0; i < m; i++){
        int p = Cin()-1;
        a[p] = 1;
    }

    for (int i = 0; i < m; i++){
        int p = Cin()-1;
        b[p] = 1;
    }

    vector<int> x, y;
    for (int i = 0; i < n; i++){
        if(a[i] == 0) x.push_back(i);
        if(b[i] == 0) y.push_back(i + n * 2);
    }

    bool gap = ((y[0] + n) - y.back() > 2);
    for (size_t i = 0; i + 1 < y.size(); i++)
        if(y[i + 1] - y[i] > 2) gap = true;

    int ans = INF;
    int sz = x.size();
    for (int t = 0; t < sz * 4; t++){
        int cost = 0, odd = 0;
        for (int i = 0; i < sz; i++){
            int d = abs(x[i] - y[i]);
            if(d & 1) odd++;
            else cost += d >> 1;
        }
        if(odd == 0 && (gap || cost == 0)) ans = min(ans, cost);
        int first = x.front();
        x.erase(x.begin());
        x.push_back(first + n);
    }
    if(ans == INF) ans = -1;
    Cout(ans);
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3532
final_rank: 2

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

int calc2(string s, string t) {
	//cout << s << " " << t << "\n";
	queue<int> qs, qt;
	int n = s.size();
	rep(i, n - 1) {
		if (s[i] == '1' && s[i + 1] == '1')qs.push(i);
		if (t[i] == '1' && t[i + 1] == '1')qt.push(i);
	}
	vector<int> rs(n + 1), rt(n + 1);
	rep(i, n) {
		rs[i + 1] = rs[i] + (s[i] == '0');
		rt[i + 1] = rt[i] + (t[i] == '0');
	}
	vector<bool> us(n), ut(n);
	int cs = 0, ct = 0;
	int res = 0;
	int pre = 0;
	while (true) {
		while (cs < n && us[cs])cs++;
		while (ct < n && ut[ct])ct++;
		if (cs == n)break;
		if (s[cs] == t[ct]) {
			us[cs] = ut[ct] = true;
			if (s[cs] == '0') {
				pre++;
			}
			cs++; ct++;
			
		}
		else {
			if (s[cs] == '1') {
				us[cs] = true;
				cs++;
				while (us[cs])cs++;
				if (s[cs] == '0')return mod;
				us[cs] = true;
				cs++;
				while (true) {
					if (qt.empty())return mod;
					int loc = qt.front(); qt.pop();
					if (ut[loc] || ut[loc + 1])continue;
					ut[loc] = ut[loc + 1] = true;
					res += rt[loc] - pre;
					break;
				}
			}
			else {
				ut[ct] = true;
				ct++;
				while (ut[ct])ct++;
				if (t[ct] == '0')return mod;
				ut[ct] = true;
				ct++;
				while (true) {
					if (qs.empty())return mod;
					int loc = qs.front(); qs.pop();
					if (us[loc] || us[loc + 1])continue;
					us[loc] = us[loc + 1] = true;
					res += rs[loc] - pre;
					break;
				}
			}
		}
	}
	return res;
}
int calc(string s, string t,int m,int chkt) {
	int n = s.size();
	if (chkt < 0) {
		if (s == t)return 0;
		return mod;
	}
	int chks = -1;
	rep(i, n) {
		int loc = chkt - i; if (loc < 0)loc += n;
		int nloc = (loc + 1) % n;
		if (s[loc] == '1' && s[nloc] == '1') {
			chks = loc; break;
		}
	}
	if (chks < 0)return mod;
	int ad = 0;
	while (chks != chkt) {
		int loc = (chks + 2) % n;
		if (s[loc] == '0')ad++;
		swap(s[chks], s[loc]);
		chks = (chks + 1) % n;
	}
	string cs, ct;
	rep(i, n - 2) {
		int loc = (chks + i + 2) % n;
		cs.push_back(s[loc]);
		ct.push_back(t[loc]);
	}
	int res = mod;
	rep(_, n) {
		//cs,ct
		int val = calc2(cs, ct);
		chmin(res, val+ad);

		ad += n - m;
		rep(j, 2) {
			cs.push_back(cs[0]);
			if (cs[0] == '0')ad++;
			cs.erase(cs.begin());
		}
	}
	return res;
}
int calcal(string s, string t) {
	//if (s == t)return 0;
	set<string> st;
	queue<string> qs;
	qs.push(s); st.insert(s);
	int tmp = 0;
	int n = s.size();
	while (!qs.empty()) {
		int len = qs.size();
		rep(_, len) {
			string cur = qs.front(); qs.pop();
			if (cur == t)return tmp;
			rep(i, n) {
				int ni = (i + 1) % n;
				if (cur[i] == '1' && cur[ni] == '1') {
					string nex = cur;
					int loc = i - 1; if (loc < 0)loc += n;
					swap(nex[loc], nex[ni]);
					if (!st.count(nex)) {
						st.insert(nex); qs.push(nex);
					}
					nex = cur;
					loc = (i + 2) % n;
					swap(nex[loc], nex[i]);
					if (!st.count(nex)) {
						st.insert(nex); qs.push(nex);
					}
				}
			}
		}
		tmp++;
	}
	return mod;
}
int calcsub(vector<int> vl, vector<int> vr, int n) {
	int ad = n;
	if (n % 2)ad = 2 * n;
	rep(i, (int)vl.size() - 1) {
		if (vl[i] > vl[i + 1]) {
			Rep(j, i + 1, vl.size()) {
				vl[j] += ad;
			}
		}
	}
	if (vl[0] > vr[0])vr[0] += n;
	/*if (n % 2) {
		int dif = vr[0] - vl[0]; if (dif < 0)dif += n;
		if (dif % 2) {
			rep(i, vr.size())vr[i] += n;
		}
	}*/
	//cout << "!!\n";
	//coutarray(vl);
	//coutarray(vr);
	int res = 0;
	//vl -> vr
	int cur = -1;
	rep(i, vl.size()) {
		int obj = vr[i];
		while (obj <= cur) {
			obj += n;
		}
		if (abs(obj - vl[i]) % 2)obj += n;
		res += abs(obj - vl[i]) / 2;
		cur = obj;
	}
	return res;
}
int calcsolve(string s, string t) {
	int chk = -1;
	rep(i, s.size()) {
		if (s[i] == '0') {
			chk = i; break;
		}
	}
	if (chk < 0) {
		return 0;
	}
	int n = s.size();
	int m = 0; rep(i, n)if (s[i] == '1')m++;
	rep(i, chk) {
		s.push_back(s[i]);
		t.push_back(t[i]);
	}
	s.erase(s.begin(), s.begin() + chk);
	t.erase(t.begin(), t.begin() + chk);
	string sl;
	vector<int> vl;
	int tmp = 0;
	rep(i, n) {
		if (s[i] == '0') {
			sl.push_back('0'); tmp++;
			vl.push_back(i);
		}
		else {
			if (i + 1 < n && s[i + 1] == '1') {
				//vl.push_back(tmp); 
				tmp++;i++;
			}
			else {
				sl.push_back('1');
			}
		}
	}
	bool exi2 = false;
	rep(i, n-1) {
		if (s[i] == '1' && s[i + 1] == '1')exi2 = true;
	}
	//cout << "? " << s << " " << t << "\n";
	int ans = mod;
	rep(i, n) {
		int loc = 2 * i % n;
		if (t[loc] == '0') {
			string sr;
			vector<int> vr;
			int tmp = 0;
			rep(j, n) {
				int id = (loc + j) % n;
				if (t[id] == '0') {
					sr.push_back('0'); tmp++;
					vr.push_back(id);
				}
				else {
					if (j + 1 < n && t[(id + 1) % n] == '1') {
						//vr.push_back(tmp); 
						tmp++; j++;
					}
					else {
						sr.push_back('1');
					}
				}
			}
			if (sl == sr) {
				int cost = min(calcsub(vl, vr,n), calcsub(vr, vl,n));
				if (!exi2) {
					if (i == 0)cost = 0;
					else cost = mod;
				}
				chmin(ans, cost);
			}
		}
	}
	return ans;
}
void solve() {
	int n, m; cin >> n >> m;
	vector<int>x(m), y(m);
	rep(i, m)cin >> x[i];
	rep(i, m)cin >> y[i];
	string s, t;
	s.resize(n, '0');
	t.resize(n, '0');
	rep(i, m) {
		s[x[i] - 1] = '1';
		t[y[i] - 1] = '1';
	}
	int ans = calcsolve(s, t);
	if (ans == mod)ans = -1;
	cout << ans << "\n";
}
int calc3(string s, string t) {
	int res = 0;
	while (s.size()) {
		if (s.back() == t.back()) {
			s.pop_back(); t.pop_back();
			continue;
		}
		if (s.back() == '1') {
			for (int i = (int)s.size() - 3;; i -= 2) {
				if (s[i + 1] == '0')return mod;
				res++;
				if (s[i] == '0') {
					swap(s[i], s.back());
					break;
				}
			}
		}
		else {
			for (int i = (int)t.size() - 3;; i -= 2) {
				if (t[i + 1] == '0')return mod;
				res++;
				if (t[i] == '0') {
					swap(t[i], t.back());
					break;
				}
			}
		}
		s.pop_back(); t.pop_back();
	}
	return res;
}
void expr() {
	int n = 5;
	rep(i, (1 << n))rep(j, (1 << n)) {
		int ci = 0, cj = 0;
		string s, t;
		s.resize(n, '0');
		t.resize(n, '0');
		rep(x, n)if (i & (1 << x))s[x]='1',ci++;
		rep(x, n)if (j & (1 << x))t[x]='1',cj++;
		if (ci == cj) {
			if (calcsolve(s, t) != calcal(s, t)) {
				cout << "? " << s << " " << t << " " << calcsolve(s, t) << " " << calcal(s, t) << "\n";
			}
		}
	}
	cout << "fin\n";
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 11792
final_rank: 7

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

int calc2(string s, string t) {
	//cout << s << " " << t << "\n";
	queue<int> qs, qt;
	int n = s.size();
	rep(i, n - 1) {
		if (s[i] == '1' && s[i + 1] == '1')qs.push(i);
		if (t[i] == '1' && t[i + 1] == '1')qt.push(i);
	}
	vector<int> rs(n + 1), rt(n + 1);
	rep(i, n) {
		rs[i + 1] = rs[i] + (s[i] == '0');
		rt[i + 1] = rt[i] + (t[i] == '0');
	}
	vector<bool> us(n), ut(n);
	int cs = 0, ct = 0;
	int res = 0;
	int pre = 0;
	while (true) {
		while (cs < n && us[cs])cs++;
		while (ct < n && ut[ct])ct++;
		if (cs == n)break;
		if (s[cs] == t[ct]) {
			us[cs] = ut[ct] = true;
			if (s[cs] == '0') {
				pre++;
			}
			cs++; ct++;
			
		}
		else {
			if (s[cs] == '1') {
				us[cs] = true;
				cs++;
				while (us[cs])cs++;
				if (s[cs] == '0')return mod;
				us[cs] = true;
				cs++;
				while (true) {
					if (qt.empty())return mod;
					int loc = qt.front(); qt.pop();
					if (ut[loc] || ut[loc + 1])continue;
					ut[loc] = ut[loc + 1] = true;
					res += rt[loc] - pre;
					break;
				}
			}
			else {
				ut[ct] = true;
				ct++;
				while (ut[ct])ct++;
				if (t[ct] == '0')return mod;
				ut[ct] = true;
				ct++;
				while (true) {
					if (qs.empty())return mod;
					int loc = qs.front(); qs.pop();
					if (us[loc] || us[loc + 1])continue;
					us[loc] = us[loc + 1] = true;
					res += rs[loc] - pre;
					break;
				}
			}
		}
	}
	return res;
}
int calc(string s, string t,int m,int chkt) {
	int n = s.size();
	if (chkt < 0) {
		if (s == t)return 0;
		return mod;
	}
	int chks = -1;
	rep(i, n) {
		int loc = chkt - i; if (loc < 0)loc += n;
		int nloc = (loc + 1) % n;
		if (s[loc] == '1' && s[nloc] == '1') {
			chks = loc; break;
		}
	}
	if (chks < 0)return mod;
	int ad = 0;
	while (chks != chkt) {
		int loc = (chks + 2) % n;
		if (s[loc] == '0')ad++;
		swap(s[chks], s[loc]);
		chks = (chks + 1) % n;
	}
	string cs, ct;
	rep(i, n - 2) {
		int loc = (chks + i + 2) % n;
		cs.push_back(s[loc]);
		ct.push_back(t[loc]);
	}
	int res = mod;
	rep(_, n) {
		//cs,ct
		int val = calc2(cs, ct);
		chmin(res, val+ad);

		ad += n - m;
		rep(j, 2) {
			cs.push_back(cs[0]);
			if (cs[0] == '0')ad++;
			cs.erase(cs.begin());
		}
	}
	return res;
}
int calcal(string s, string t) {
	//if (s == t)return 0;
	set<string> st;
	queue<string> qs;
	qs.push(s); st.insert(s);
	int tmp = 0;
	int n = s.size();
	while (!qs.empty()) {
		int len = qs.size();
		rep(_, len) {
			string cur = qs.front(); qs.pop();
			if (cur == t)return tmp;
			rep(i, n) {
				int ni = (i + 1) % n;
				if (cur[i] == '1' && cur[ni] == '1') {
					string nex = cur;
					int loc = i - 1; if (loc < 0)loc += n;
					swap(nex[loc], nex[ni]);
					if (!st.count(nex)) {
						st.insert(nex); qs.push(nex);
					}
					nex = cur;
					loc = (i + 2) % n;
					swap(nex[loc], nex[i]);
					if (!st.count(nex)) {
						st.insert(nex); qs.push(nex);
					}
				}
			}
		}
		tmp++;
	}
	return mod;
}
int calcsub(vector<int> vl, vector<int> vr, int n) {
	int ad = n;
	if (n % 2)ad = 2 * n;
	rep(i, (int)vl.size() - 1) {
		if (vl[i] > vl[i + 1]) {
			Rep(j, i + 1, vl.size()) {
				vl[j] += ad;
			}
		}
	}
	if (vl[0] > vr[0])vr[0] += ad;
	//cout << "!!\n";
	//coutarray(vl);
	//coutarray(vr);
	int res = 0;
	//vl -> vr
	int cur = -1;
	rep(i, vl.size()) {
		int obj = vr[i];
		while (obj <= cur) {
			obj += n;
		}
		res += abs(obj - vl[i]) / 2;
		cur = obj;
	}
	return res;
}
int calcsolve(string s, string t) {
	int chk = -1;
	rep(i, s.size()) {
		if (s[i] == '0') {
			chk = i; break;
		}
	}
	if (chk < 0) {
		return 0;
	}
	int n = s.size();
	int m = 0; rep(i, n)if (s[i] == '1')m++;
	rep(i, chk) {
		s.push_back(s[i]);
		t.push_back(t[i]);
	}
	s.erase(s.begin(), s.begin() + chk);
	t.erase(t.begin(), t.begin() + chk);
	string sl;
	vector<int> vl;
	int tmp = 0;
	rep(i, n) {
		if (s[i] == '0') {
			sl.push_back('0'); tmp++;
			vl.push_back(i);
		}
		else {
			if (i + 1 < n && s[i + 1] == '1') {
				//vl.push_back(tmp); 
				tmp++;i++;
			}
			else {
				sl.push_back('1');
			}
		}
	}
	bool exi2 = false;
	rep(i, n-1) {
		if (s[i] == '1' && s[i + 1] == '1')exi2 = true;
	}
	//cout << "? " << s << " " << t << "\n";
	int ans = mod;
	rep(i, n) {
		int loc = 2 * i % n;
		if (t[loc] == '0') {
			string sr;
			vector<int> vr;
			int tmp = 0;
			rep(j, n) {
				int id = (loc + j) % n;
				if (t[id] == '0') {
					sr.push_back('0'); tmp++;
					vr.push_back(id);
				}
				else {
					if (j + 1 < n && t[(id + 1) % n] == '1') {
						//vr.push_back(tmp); 
						tmp++; j++;
					}
					else {
						sr.push_back('1');
					}
				}
			}
			if (sl == sr) {
				int cost = min(calcsub(vl, vr,n), calcsub(vr, vl,n));
				if (!exi2) {
					if (i == 0)cost = 0;
					else cost = mod;
				}
				chmin(ans, cost);
			}
		}
	}
	return ans;
}
void solve() {
	int n, m; cin >> n >> m;
	vector<int>x(m), y(m);
	rep(i, m)cin >> x[i];
	rep(i, m)cin >> y[i];
	string s, t;
	s.resize(n, '0');
	t.resize(n, '0');
	rep(i, m) {
		s[x[i] - 1] = '1';
		t[y[i] - 1] = '1';
	}
	int ans = calcsolve(s, t);
	if (ans == mod)ans = -1;
	cout << ans << "\n";
}
int calc3(string s, string t) {
	int res = 0;
	while (s.size()) {
		if (s.back() == t.back()) {
			s.pop_back(); t.pop_back();
			continue;
		}
		if (s.back() == '1') {
			for (int i = (int)s.size() - 3;; i -= 2) {
				if (s[i + 1] == '0')return mod;
				res++;
				if (s[i] == '0') {
					swap(s[i], s.back());
					break;
				}
			}
		}
		else {
			for (int i = (int)t.size() - 3;; i -= 2) {
				if (t[i + 1] == '0')return mod;
				res++;
				if (t[i] == '0') {
					swap(t[i], t.back());
					break;
				}
			}
		}
		s.pop_back(); t.pop_back();
	}
	return res;
}
void expr() {
	int n = 10;
	rep(i, (1 << n))rep(j, (1 << n)) {
		int ci = 0, cj = 0;
		string s, t;
		s.resize(n, '0');
		t.resize(n, '0');
		rep(x, n)if (i & (1 << x))s[x]='1',ci++;
		rep(x, n)if (j & (1 << x))t[x]='1',cj++;
		if (ci == cj) {
			if (calcsolve(s, t) != calcal(s, t)) {
				cout << "? " << s << " " << t << " " << calcsolve(s, t) << " " << calcal(s, t) << "\n";
			}
		}
	}
	cout << "fin\n";
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.037037037037037035
time_ms: 30
memory_kb: 11648
final_rank: 20

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

int calc2(string s, string t) {
	//cout << s << " " << t << "\n";
	queue<int> qs, qt;
	int n = s.size();
	rep(i, n - 1) {
		if (s[i] == '1' && s[i + 1] == '1')qs.push(i);
		if (t[i] == '1' && t[i + 1] == '1')qt.push(i);
	}
	vector<int> rs(n + 1), rt(n + 1);
	rep(i, n) {
		rs[i + 1] = rs[i] + (s[i] == '0');
		rt[i + 1] = rt[i] + (t[i] == '0');
	}
	vector<bool> us(n), ut(n);
	int cs = 0, ct = 0;
	int res = 0;
	int pre = 0;
	while (true) {
		while (cs < n && us[cs])cs++;
		while (ct < n && ut[ct])ct++;
		if (cs == n)break;
		if (s[cs] == t[ct]) {
			us[cs] = ut[ct] = true;
			if (s[cs] == '0') {
				pre++;
			}
			cs++; ct++;
			
		}
		else {
			if (s[cs] == '1') {
				us[cs] = true;
				cs++;
				while (us[cs])cs++;
				if (s[cs] == '0')return mod;
				us[cs] = true;
				cs++;
				while (true) {
					if (qt.empty())return mod;
					int loc = qt.front(); qt.pop();
					if (ut[loc] || ut[loc + 1])continue;
					ut[loc] = ut[loc + 1] = true;
					res += rt[loc] - pre;
					break;
				}
			}
			else {
				ut[ct] = true;
				ct++;
				while (ut[ct])ct++;
				if (t[ct] == '0')return mod;
				ut[ct] = true;
				ct++;
				while (true) {
					if (qs.empty())return mod;
					int loc = qs.front(); qs.pop();
					if (us[loc] || us[loc + 1])continue;
					us[loc] = us[loc + 1] = true;
					res += rs[loc] - pre;
					break;
				}
			}
		}
	}
	return res;
}
int calc(string s, string t,int m,int chkt) {
	int n = s.size();
	if (chkt < 0) {
		if (s == t)return 0;
		return mod;
	}
	int chks = -1;
	rep(i, n) {
		int loc = chkt - i; if (loc < 0)loc += n;
		int nloc = (loc + 1) % n;
		if (s[loc] == '1' && s[nloc] == '1') {
			chks = loc; break;
		}
	}
	if (chks < 0)return mod;
	int ad = 0;
	while (chks != chkt) {
		int loc = (chks + 2) % n;
		if (s[loc] == '0')ad++;
		swap(s[chks], s[loc]);
		chks = (chks + 1) % n;
	}
	string cs, ct;
	rep(i, n - 2) {
		int loc = (chks + i + 2) % n;
		cs.push_back(s[loc]);
		ct.push_back(t[loc]);
	}
	int res = mod;
	rep(_, n) {
		//cs,ct
		int val = calc2(cs, ct);
		chmin(res, val+ad);

		ad += n - m;
		rep(j, 2) {
			cs.push_back(cs[0]);
			if (cs[0] == '0')ad++;
			cs.erase(cs.begin());
		}
	}
	return res;
}
void solve() {
	int n, m; cin >> n >> m;
	vector<int>x(m), y(m);
	rep(i, m)cin >> x[i];
	rep(i, m)cin >> y[i];
	string s, t;
	s.resize(n, '0');
	t.resize(n, '0');
	rep(i, m) {
		s[x[i] - 1] = '1';
		t[y[i] - 1] = '1';
	}
	int chkt = -1;
	rep(i, n) {
		int ni = (i + 1) % n;
		if (t[i] == '1' && t[ni] == '1') {
			chkt = i; break;
		}
	}
	int ans1 = calc(s, t, m,chkt);
	reverse(all(s));
	reverse(all(t));
	chkt++;
	chkt %= n;
	chkt = n - 1 - chkt;
	int ans2 = calc(s, t, m,chkt);
	int ans = min(ans1, ans2);
	if (ans == mod)ans = -1;
	cout << ans << "\n";
}
int calc3(string s, string t) {
	int res = 0;
	while (s.size()) {
		if (s.back() == t.back()) {
			s.pop_back(); t.pop_back();
			continue;
		}
		if (s.back() == '1') {
			for (int i = (int)s.size() - 3;; i -= 2) {
				if (s[i + 1] == '0')return mod;
				res++;
				if (s[i] == '0') {
					swap(s[i], s.back());
					break;
				}
			}
		}
		else {
			for (int i = (int)t.size() - 3;; i -= 2) {
				if (t[i + 1] == '0')return mod;
				res++;
				if (t[i] == '0') {
					swap(t[i], t.back());
					break;
				}
			}
		}
		s.pop_back(); t.pop_back();
	}
	return res;
}
void expr() {
	int n = 10;
	rep(i, (1 << n))rep(j, (1 << n)) {
		int ci = 0, cj = 0;
		string s, t;
		s.resize(n, '0');
		t.resize(n, '0');
		rep(x, n)if (i & (1 << x))s[x]='1',ci++;
		rep(x, n)if (j & (1 << x))t[x]='1',cj++;
		if (ci == cj) {
			if (calc2(s, t) != calc3(s, t)) {
				cout << "? " << s << " " << t << " " << calc2(s, t) << " " << calc3(s, t) << "\n";
			}
		}
	}
	cout << "fin\n";
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.2962962962962963
time_ms: 120
memory_kb: 11728
final_rank: 12

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

int calc2(string s, string t) {
	//cout << s << " " << t << "\n";
	queue<int> qs, qt;
	int n = s.size();
	rep(i, n - 1) {
		if (s[i] == '1' && s[i + 1] == '1')qs.push(i);
		if (t[i] == '1' && t[i + 1] == '1')qt.push(i);
	}
	vector<int> rs(n + 1), rt(n + 1);
	rep(i, n) {
		rs[i + 1] = rs[i] + (s[i] == '0');
		rt[i + 1] = rt[i] + (t[i] == '0');
	}
	vector<bool> us(n), ut(n);
	int cs = 0, ct = 0;
	int res = 0;
	int pre = 0;
	while (true) {
		while (cs < n && us[cs])cs++;
		while (ct < n && ut[ct])ct++;
		if (cs == n)break;
		if (s[cs] == t[ct]) {
			us[cs] = ut[ct] = true;
			if (s[cs] == '0') {
				pre++;
			}
			cs++; ct++;
			
		}
		else {
			if (s[cs] == '1') {
				us[cs] = true;
				cs++;
				while (us[cs])cs++;
				if (s[cs] == '0')return mod;
				us[cs] = true;
				cs++;
				while (true) {
					if (qt.empty())return mod;
					int loc = qt.front(); qt.pop();
					if (ut[loc] || ut[loc + 1])continue;
					ut[loc] = ut[loc + 1] = true;
					res += rt[loc] - pre;
					break;
				}
			}
			else {
				ut[ct] = true;
				ct++;
				while (ut[ct])ct++;
				if (t[ct] == '0')return mod;
				ut[ct] = true;
				ct++;
				while (true) {
					if (qs.empty())return mod;
					int loc = qs.front(); qs.pop();
					if (us[loc] || us[loc + 1])continue;
					us[loc] = us[loc + 1] = true;
					res += rs[loc] - pre;
					break;
				}
			}
		}
	}
	return res;
}
int calc(string s, string t,int m,int chkt) {
	int n = s.size();
	if (chkt < 0) {
		if (s == t)return 0;
		return mod;
	}
	int chks = -1;
	rep(i, n) {
		int loc = chkt - i; if (loc < 0)loc += n;
		int nloc = (loc + 1) % n;
		if (s[loc] == '1' && s[nloc] == '1') {
			chks = loc; break;
		}
	}
	if (chks < 0)return mod;
	int ad = 0;
	while (chks != chkt) {
		int loc = (chks + 2) % n;
		if (s[loc] == '0')ad++;
		swap(s[chks], s[loc]);
		chks = (chks + 1) % n;
	}
	string cs, ct;
	rep(i, n - 2) {
		int loc = (chks + i + 2) % n;
		cs.push_back(s[loc]);
		ct.push_back(t[loc]);
	}
	int res = mod;
	rep(_, n) {
		//cs,ct
		int val = calc2(cs, ct);
		chmin(res, val+ad);

		ad += n - m;
		rep(j, 2) {
			cs.push_back(cs[0]);
			if (cs[0] == '0')ad++;
			cs.erase(cs.begin());
		}
	}
	return res;
}
void solve() {
	int n, m; cin >> n >> m;
	vector<int>x(m), y(m);
	rep(i, m)cin >> x[i];
	rep(i, m)cin >> y[i];
	string s, t;
	s.resize(n, '0');
	t.resize(n, '0');
	rep(i, m) {
		s[x[i] - 1] = '1';
		t[y[i] - 1] = '1';
	}
	int chkt = -1;
	rep(i, n) {
		int ni = (i + 1) % n;
		if (t[i] == '1' && t[ni] == '1') {
			chkt = i;
		}
	}
	int ans1 = calc(s, t, m,chkt);
	reverse(all(s));
	reverse(all(t));
	chkt++;
	chkt %= n;
	chkt = n - 1 - chkt;
	int ans2 = calc(s, t, m,chkt);
	int ans = min(ans1, ans2);
	if (ans == mod)ans = -1;
	cout << ans << "\n";
}
int calc3(string s, string t) {
	int res = 0;
	while (s.size()) {
		if (s.back() == t.back()) {
			s.pop_back(); t.pop_back();
			continue;
		}
		if (s.back() == '1') {
			for (int i = (int)s.size() - 3;; i -= 2) {
				if (s[i + 1] == '0')return mod;
				res++;
				if (s[i] == '0') {
					swap(s[i], s.back());
					break;
				}
			}
		}
		else {
			for (int i = (int)t.size() - 3;; i -= 2) {
				if (t[i + 1] == '0')return mod;
				res++;
				if (t[i] == '0') {
					swap(t[i], t.back());
					break;
				}
			}
		}
		s.pop_back(); t.pop_back();
	}
	return res;
}
void expr() {
	int n = 6;
	rep(i, (1 << n))rep(j, (1 << n)) {
		int ci = 0, cj = 0;
		string s, t;
		s.resize(n, '0');
		t.resize(n, '0');
		rep(x, n)if (i & (1 << x))s[x]='1',ci++;
		rep(x, n)if (j & (1 << x))t[x]='1',cj++;
		if (ci == cj) {
			if (calc2(s, t) != calc3(s, t)) {
				cout << "? " << s << " " << t << " " << calc2(s, t) << " " << calc3(s, t) << "\n";
			}
		}
	}
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.2777777777777778
time_ms: 120
memory_kb: 11724
final_rank: 13

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

int calc2(string s, string t) {
	//cout << s << " " << t << "\n";
	queue<int> qs, qt;
	int n = s.size();
	rep(i, n - 1) {
		if (s[i] == '1' && s[i + 1] == '1')qs.push(i);
		if (t[i] == '1' && t[i + 1] == '1')qt.push(i);
	}
	vector<int> rs(n + 1), rt(n + 1);
	rep(i, n) {
		rs[i + 1] = rs[i] + (s[i] == '0');
		rt[i + 1] = rt[i] + (t[i] == '0');
	}
	vector<bool> us(n), ut(n);
	int cs = 0, ct = 0;
	int res = 0;
	int pre = 0;
	while (true) {
		while (cs < n && us[cs])cs++;
		while (ct < n && ut[ct])ct++;
		if (cs == n)break;
		if (s[cs] == t[ct]) {
			us[cs] = ut[ct] = true;
			cs++; ct++;
			if (s[cs] == '0') {
				pre++;
			}
		}
		else {
			if (s[cs] == '1') {
				us[cs] = true;
				cs++;
				while (us[cs])cs++;
				if (s[cs] == '0')return mod;
				us[cs] = true;
				cs++;
				while (true) {
					if (qt.empty())return mod;
					int loc = qt.front(); qt.pop();
					if (ut[loc] || ut[loc + 1])continue;
					ut[loc] = ut[loc + 1] = true;
					res += rt[loc] - pre;
					break;
				}
			}
			else {
				ut[ct] = true;
				ct++;
				while (ut[ct])ct++;
				if (t[ct] == '0')return mod;
				ut[ct] = true;
				ct++;
				while (true) {
					if (qs.empty())return mod;
					int loc = qs.front(); qs.pop();
					if (us[loc] || us[loc + 1])continue;
					us[loc] = us[loc + 1] = true;
					res += rs[loc] - pre;
					break;
				}
			}
		}
	}
	return res;
}
int calc(string s, string t,int m,int chkt) {
	int n = s.size();
	if (chkt < 0) {
		if (s == t)return 0;
		return mod;
	}
	int chks = -1;
	rep(i, n) {
		int loc = chkt - i; if (loc < 0)loc += n;
		int nloc = (loc + 1) % n;
		if (s[loc] == '1' && s[nloc] == '1') {
			chks = loc; break;
		}
	}
	if (chks < 0)return mod;
	int ad = 0;
	while (chks != chkt) {
		int loc = (chks + 2) % n;
		if (s[loc] == '0')ad++;
		swap(s[chks], s[loc]);
		chks = (chks + 1) % n;
	}
	string cs, ct;
	rep(i, n - 2) {
		int loc = (chks + i + 2) % n;
		cs.push_back(s[loc]);
		ct.push_back(t[loc]);
	}
	int res = mod;
	rep(_, n) {
		//cs,ct
		int val = calc2(cs, ct);
		chmin(res, val+ad);

		ad += n - m;
		rep(j, 2) {
			cs.push_back(cs[0]);
			if (cs[0] == '0')ad++;
			cs.erase(cs.begin());
		}
	}
	return res;
}
void solve() {
	int n, m; cin >> n >> m;
	vector<int>x(m), y(m);
	rep(i, m)cin >> x[i];
	rep(i, m)cin >> y[i];
	string s, t;
	s.resize(n, '0');
	t.resize(n, '0');
	rep(i, m) {
		s[x[i] - 1] = '1';
		t[y[i] - 1] = '1';
	}
	int chkt = -1;
	rep(i, n) {
		int ni = (i + 1) % n;
		if (t[i] == '1' && t[ni] == '1') {
			chkt = i;
		}
	}
	int ans1 = calc(s, t, m,chkt);
	reverse(all(s));
	reverse(all(t));
	chkt++;
	chkt %= n;
	chkt = n - 1 - chkt;
	int ans2 = calc(s, t, m,chkt);
	int ans = min(ans1, ans2);
	if (ans == mod)ans = -1;
	cout << ans << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.2777777777777778
time_ms: 120
memory_kb: 11772
final_rank: 14

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

int calc2(string s, string t) {
	//cout << s << " " << t << "\n";
	queue<int> qs, qt;
	int n = s.size();
	rep(i, n - 1) {
		if (s[i] == '1' && s[i + 1] == '1')qs.push(i);
		if (t[i] == '1' && t[i + 1] == '1')qt.push(i);
	}
	vector<int> rs(n + 1), rt(n + 1);
	rep(i, n) {
		rs[i + 1] = rs[i] + (s[i] == '0');
		rt[i + 1] = rt[i] + (t[i] == '0');
	}
	vector<bool> us(n), ut(n);
	int cs = 0, ct = 0;
	int res = 0;
	int pre = 0;
	while (true) {
		while (cs < n && us[cs])cs++;
		while (ct < n && ut[ct])ct++;
		if (cs == n)break;
		if (s[cs] == t[ct]) {
			us[cs] = ut[ct] = true;
			cs++; ct++;
			if (s[cs] == '0') {
				pre++;
			}
		}
		else {
			if (s[cs] == '1') {
				us[cs] = true;
				cs++;
				while (us[cs])cs++;
				if (s[cs] == '0')return mod;
				us[cs] = true;
				cs++;
				while (true) {
					if (qt.empty())return mod;
					int loc = qt.front(); qt.pop();
					if (ut[loc] || ut[loc + 1])continue;
					ut[loc] = ut[loc + 1] = true;
					res += rt[loc] - pre;
					break;
				}
			}
			else {
				ut[ct] = true;
				ct++;
				while (ut[ct])ct++;
				if (t[ct] == '0')return mod;
				ut[ct] = true;
				ct++;
				while (true) {
					if (qs.empty())return mod;
					int loc = qs.front(); qs.pop();
					if (us[loc] || us[loc + 1])continue;
					us[loc] = us[loc + 1] = true;
					res += rs[loc] - pre;
					break;
				}
			}
		}
	}
	return res;
}
int calc(string s, string t,int m) {
	int n = s.size();
	int chkt = -1;
	rep(i, n) {
		int ni = (i + 1) % n;
		if (t[i] == '1' && t[ni] == '1') {
			chkt = i; break;
		}
	}
	if (chkt < 0) {
		if (s == t)return 0;
		return mod;
	}
	int chks = -1;
	rep(i, n) {
		int loc = chkt - i; if (loc < 0)loc += n;
		int nloc = (loc + 1) % n;
		if (s[loc] == '1' && s[nloc] == '1') {
			chks = loc; break;
		}
	}
	if (chks < 0)return mod;
	int ad = 0;
	while (chks != chkt) {
		int loc = (chks + 2) % n;
		if (s[loc] == '0')ad++;
		swap(s[chks], s[loc]);
		chks = (chks + 1) % n;
	}
	string cs, ct;
	rep(i, n - 2) {
		int loc = (chks + i + 2) % n;
		cs.push_back(s[loc]);
		ct.push_back(t[loc]);
	}
	int res = mod;
	rep(_, 2*n) {
		//cs,ct
		int val = calc2(cs, ct);
		chmin(res, val+ad);

		ad += n - m;
		rep(j, 2) {
			cs.push_back(cs[0]);
			if (cs[0] == '0')ad++;
			cs.erase(cs.begin());
		}
	}
	return res;
}
void solve() {
	int n, m; cin >> n >> m;
	vector<int>x(m), y(m);
	rep(i, m)cin >> x[i];
	rep(i, m)cin >> y[i];
	string s, t;
	s.resize(n, '0');
	t.resize(n, '0');
	rep(i, m) {
		s[x[i] - 1] = '1';
		t[y[i] - 1] = '1';
	}
	int ans1 = calc(s, t, m);
	reverse(all(s));
	reverse(all(t));
	int ans2 = calc(s, t, m);
	int ans = min(ans1, ans2);
	if (ans == mod)ans = -1;
	cout << ans << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.2777777777777778
time_ms: 270
memory_kb: 11724
final_rank: 16

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

int calc2(string s, string t) {
	//cout << s << " " << t << "\n";
	queue<int> qs, qt;
	int n = s.size();
	rep(i, n - 1) {
		if (s[i] == '1' && s[i + 1] == '1')qs.push(i);
		if (t[i] == '1' && t[i + 1] == '1')qt.push(i);
	}
	vector<int> rs(n + 1), rt(n + 1);
	rep(i, n) {
		rs[i + 1] = rs[i] + (s[i] == '0');
		rt[i + 1] = rt[i] + (t[i] == '0');
	}
	vector<bool> us(n), ut(n);
	int cs = 0, ct = 0;
	int res = 0;
	int pre = 0;
	while (true) {
		while (cs < n && us[cs])cs++;
		while (ct < n && ut[ct])ct++;
		if (cs == n)break;
		if (s[cs] == t[ct]) {
			us[cs] = ut[ct] = true;
			cs++; ct++;
			if (s[cs] == '0') {
				pre++;
			}
		}
		else {
			if (s[cs] == '1') {
				us[cs] = true;
				cs++;
				while (us[cs])cs++;
				if (s[cs] == '0')return mod;
				us[cs] = true;
				cs++;
				while (true) {
					if (qt.empty())return mod;
					int loc = qt.front(); qt.pop();
					if (ut[loc] || ut[loc + 1])continue;
					ut[loc] = ut[loc + 1] = true;
					res += rt[loc] - pre;
					break;
				}
			}
			else {
				ut[ct] = true;
				ct++;
				while (ut[ct])ct++;
				if (t[ct] == '0')return mod;
				ut[ct] = true;
				ct++;
				while (true) {
					if (qs.empty())return mod;
					int loc = qs.front(); qs.pop();
					if (us[loc] || us[loc + 1])continue;
					us[loc] = us[loc + 1] = true;
					res += rs[loc] - pre;
					break;
				}
			}
		}
	}
	return res;
}
int calc(string s, string t,int m) {
	int n = s.size();
	int chkt = -1;
	rep(i, n) {
		int ni = (i + 1) % n;
		if (t[i] == '1' && t[ni] == '1') {
			chkt = i; break;
		}
	}
	if (chkt < 0) {
		if (s == t)return 0;
		return mod;
	}
	int chks = -1;
	rep(i, n) {
		int loc = chkt - i; if (loc < 0)loc += n;
		int nloc = (loc + 1) % n;
		if (s[loc] == '1' && s[nloc] == '1') {
			chks = loc; break;
		}
	}
	if (chks < 0)return mod;
	int ad = 0;
	while (chks != chkt) {
		int loc = (chks + 2) % n;
		if (s[loc] == '0')ad++;
		swap(s[chks], s[loc]);
		chks = (chks + 1) % n;
	}
	string cs, ct;
	rep(i, n - 2) {
		int loc = (chks + i + 2) % n;
		cs.push_back(s[loc]);
		ct.push_back(t[loc]);
	}
	int res = mod;
	rep(_, n) {
		//cs,ct
		int val = calc2(cs, ct);
		chmin(res, val+ad);

		ad += n - m;
		if (cs[0] == '0')ad++;
		if (cs[1 % cs.size()] == '0')ad++;
		rep(j, 2) {
			cs.push_back(cs[0]);
			cs.erase(cs.begin());
		}
	}
	return res;
}
void solve() {
	int n, m; cin >> n >> m;
	vector<int>x(m), y(m);
	rep(i, m)cin >> x[i];
	rep(i, m)cin >> y[i];
	string s, t;
	s.resize(n, '0');
	t.resize(n, '0');
	rep(i, m) {
		s[x[i] - 1] = '1';
		t[y[i] - 1] = '1';
	}
	int ans1 = calc(s, t, m);
	reverse(all(s));
	reverse(all(t));
	int ans2 = calc(s, t, m);
	int ans = min(ans1, ans2);
	if (ans == mod)ans = -1;
	cout << ans << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.2777777777777778
time_ms: 130
memory_kb: 11740
final_rank: 15

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 3005

enum Type{
	FROM,
	TO,
};

ll N,M;
ll SIZE;
bool check_from[NUM],check_to[NUM];
vector<ll> V[2],work[2];
ll MOVE[NUM],ADD[NUM],LOC[NUM];


int main(){

	scanf("%lld %lld",&N,&M);

	for(ll i = 0; i < N; i++){

		check_from[i] = false;
		check_to[i] = false;
	}

	ll tmp;

	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_from[tmp] = true;
	}
	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_to[tmp] = true;
	}

	if(M == N){

		printf("0\n");
		return 0;
	}

	for(ll i = 0; i < N; i++){
		if(!check_from[i]){

			V[FROM].push_back(i);
		}
		if(!check_to[i]){

			V[TO].push_back(i);
		}
	}

	bool FLG = false;
	SIZE = N-M;

	for(ll i = 0; i < SIZE; i++){

		if(abs(V[FROM][(i+1)%SIZE]-V[FROM][i]+N)%N >= 3){

			FLG = true;
			break;
		}
	}

	if(!FLG && N-M > 1){

		for(ll i = 0; i < SIZE; i++){
			if(V[FROM][i] != V[TO][i]){

				printf("-1\n");
				return 0;
			}
		}

		printf("0\n");
		return 0;
	}

	ll ans = HUGE_NUM;

	for(ll i = 0; i < SIZE; i++){
		for(int loop = 0; loop < 2; loop++){

			if(loop == 0){

				work[FROM] = V[FROM];
				work[TO] = V[TO];
			}else{

				work[FROM] = V[TO];
				work[TO] = V[FROM];
			}

			/*FROM→TOへ右に動く*/

			if(work[TO][i] >= work[FROM][0]){

				MOVE[0] = work[TO][i]-work[FROM][0]; //どれだけ動くか

			}else{ //work[TO][i] < work[FROM][0]

				MOVE[0] = work[TO][i]-work[FROM][0]+N;
			}

			ADD[0] = MOVE[0];
			LOC[0] = ADD[0]+work[FROM][0]; //周回を考慮した位置

			FLG = true;

			//少なくとも1つ奇数動があるか調べる
			for(ll k = 1; k < SIZE; k++){
				if(abs(MOVE[k-1])%2 == 1){

					FLG = false;
					break;
				}

				if(LOC[k-1] >= work[FROM][k]){ //1つ左の移動先が、自分より右に位置する場合

					ADD[k] = LOC[k-1]-work[FROM][k]+1; //少なくとも、1つ左の移動先よりも,1つ右に位置できる分動く必要あり

					tmp = (ADD[k]+work[FROM][k])%N;
					MOVE[k] = ADD[k]+abs(work[TO][(i+k)%SIZE]-tmp+N)%N;

					LOC[k] = work[FROM][k]+MOVE[k];

				}else{ //1つ左の移動先が、自分より左に位置する場合


					if(work[TO][(i+k)%SIZE] >= work[FROM][k]){ //移動先が自分より右

						MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];

					}else{ //移動先が自分より左

						MOVE[k] = (work[TO][(i+k)%SIZE]-work[FROM][k]+N)%N;

						if(work[TO][(i+k)%SIZE] > LOC[k-1]){//左に動かねばらならない?(右に動くと周回で1つ左の空白を抜く)

							MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];
						}
					}

					LOC[k] = work[FROM][k]+MOVE[k];
				}
			}

			if(FLG && abs(MOVE[SIZE-1])%2 == 1){

				FLG = false;
			}

			ll tmp_sum = 0;

			if(FLG){ //奇数動なし

				for(ll k = 0; k < SIZE; k++){

					tmp_sum += abs(MOVE[k])/2;
				}
				ans = min(ans,tmp_sum);

			}else{ //奇数動あり

				if(N%2 == 0)continue;

				//Nが奇数ならもう1周してみる
				for(ll k = 0; k < SIZE; k++){

					work[TO][k] += N;
				}

				FLG = true;

				MOVE[0] = work[TO][i]-work[FROM][0];

				//少なくとも1つ奇数動があるか調べる
				for(ll k = 1; k < SIZE; k++){
					if(abs(MOVE[k-1])%2 == 1){

						FLG = false;
						break;
					}

					if(LOC[k-1] >= work[FROM][k]){ //1つ左の移動先が、自分より右に位置する場合

						ADD[k] = LOC[k-1]-work[FROM][k]+1; //少なくとも、1つ左の移動先よりも,1つ右に位置できる分動く必要あり

						tmp = (ADD[k]+work[FROM][k])%N;
						MOVE[k] = ADD[k]+abs(work[TO][(i+k)%SIZE]-tmp+N)%N;

						LOC[k] = work[FROM][k]+MOVE[k];

					}else{ //1つ左の移動先が、自分より左に位置する場合


						if(work[TO][(i+k)%SIZE] >= work[FROM][k]){ //移動先が自分より右

							MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];

						}else{ //移動先が自分より左

							MOVE[k] = (work[TO][(i+k)%SIZE]-work[FROM][k]+N)%N;

							if(work[TO][(i+k)%SIZE] > LOC[k-1]){//左に動かねばらならない?(右に動くと周回で1つ左の空白を抜く)

								MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];
							}
						}

						LOC[k] = work[FROM][k]+MOVE[k];
					}
				}

				if(FLG && abs(MOVE[SIZE-1])%2 == 1){

					FLG = false;
				}

				if(FLG){

					for(ll k = 0; k < SIZE; k++){

						tmp_sum += abs(MOVE[k])/2;
					}
					ans = min(ans,tmp_sum);
				}
			}
		}
	}

	if(ans == HUGE_NUM){

		printf("-1\n");
	}else{

		printf("%lld\n",ans);
	}

	return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 3496
final_rank: 5

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 3005

enum Type{
	FROM,
	TO,
};

ll N,M;
ll SIZE;
bool check_from[NUM],check_to[NUM];
vector<ll> V[2],work[2];
ll MOVE[NUM],ADD[NUM],LOC[NUM];


int main(){

	scanf("%lld %lld",&N,&M);

	for(ll i = 0; i < N; i++){

		check_from[i] = false;
		check_to[i] = false;
	}

	ll tmp;

	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_from[tmp] = true;
	}
	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_to[tmp] = true;
	}

	if(M == N){

		printf("0\n");
		return 0;
	}

	for(ll i = 0; i < N; i++){
		if(!check_from[i]){

			V[FROM].push_back(i);
		}
		if(!check_to[i]){

			V[TO].push_back(i);
		}
	}

	bool FLG = false;
	SIZE = N-M;

	for(ll i = 0; i < SIZE; i++){

		if(abs(V[FROM][(i+1)%SIZE]-V[FROM][i]+N)%N >= 3){

			FLG = true;
			break;
		}
	}

	if(!FLG && N-M > 1){

		for(ll i = 0; i < SIZE; i++){
			if(V[FROM][i] != V[TO][i]){

				printf("-1\n");
				return 0;
			}
		}

		printf("0\n");
		return 0;
	}

	ll ans = HUGE_NUM;

	for(ll i = 0; i < SIZE; i++){
		for(int loop = 0; loop < 2; loop++){

			if(loop == 0){

				work[FROM] = V[FROM];
				work[TO] = V[TO];
			}else{

				work[FROM] = V[TO];
				work[TO] = V[FROM];
			}

			/*FROM→TOへ右に動く*/

			if(work[TO][i] >= work[FROM][0]){

				MOVE[0] = work[TO][i]-work[FROM][0]; //どれだけ動くか

			}else{ //work[TO][i] < work[FROM][0]

				MOVE[0] = work[TO][i]-work[FROM][0]+N;
			}

			ADD[0] = MOVE[0];
			LOC[0] = ADD[0]+work[FROM][0]; //周回を考慮した位置

			FLG = true;

			//少なくとも1つ奇数動があるか調べる
			for(ll k = 1; k < SIZE; k++){
				if(abs(MOVE[k-1])%2 == 1){

					FLG = false;
					break;
				}

				if(LOC[k-1] >= work[FROM][k]){ //1つ左の移動先が、自分より右に位置する場合

					ADD[k] = LOC[k-1]-work[FROM][k]+1; //少なくとも、1つ左の移動先よりも,1つ右に位置できる分動く必要あり

					tmp = (ADD[k]+work[FROM][k])%N;
					MOVE[k] = ADD[k]+abs(work[TO][(i+k)%SIZE]-tmp+N)%N;

					LOC[k] = work[FROM][k]+MOVE[k];

				}else{ //1つ左の移動先が、自分より左に位置する場合


					if(work[TO][(i+k)%SIZE] >= work[FROM][k]){ //移動先が自分より右

						MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];

					}else{ //移動先が自分より左

						MOVE[k] = (work[TO][(i+k)%SIZE]-work[FROM][k]+N)%N;

						if(work[TO][(i+k)%SIZE] > LOC[k-1]){//左に動かねばらならない?(右に動くと周回で1つ左の空白を抜く)

							MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];
						}
					}

					LOC[k] = work[FROM][k]+MOVE[k];
				}
			}

			if(FLG && abs(MOVE[SIZE-1])%2 == 1){

				FLG = false;
			}

			ll tmp_sum = 0;

			if(FLG){ //奇数動なし

				for(ll k = 0; k < SIZE; k++){

					tmp_sum += abs(MOVE[k])/2;
				}
				ans = min(ans,tmp_sum);

			}else{ //奇数動あり

				if(N%2 == 0)continue;

				//Nが奇数ならもう1周してみる
				work[TO][0] += N;
				/*for(ll k = 0; k < SIZE; k++){

					work[TO][k] += N;
				}*/

				FLG = true;

				MOVE[0] = work[TO][i]-work[FROM][0];

				//少なくとも1つ奇数動があるか調べる
				for(ll k = 1; k < SIZE; k++){
					if(abs(MOVE[k-1])%2 == 1){

						FLG = false;
						break;
					}

					if(LOC[k-1] >= work[FROM][k]){ //1つ左の移動先が、自分より右に位置する場合

						ADD[k] = LOC[k-1]-work[FROM][k]+1; //少なくとも、1つ左の移動先よりも,1つ右に位置できる分動く必要あり

						tmp = (ADD[k]+work[FROM][k])%N;
						MOVE[k] = ADD[k]+abs(work[TO][(i+k)%SIZE]-tmp+N)%N;

						LOC[k] = work[FROM][k]+MOVE[k];

					}else{ //1つ左の移動先が、自分より左に位置する場合


						if(work[TO][(i+k)%SIZE] >= work[FROM][k]){ //移動先が自分より右

							MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];

						}else{ //移動先が自分より左

							MOVE[k] = (work[TO][(i+k)%SIZE]-work[FROM][k]+N)%N;

							if(work[TO][(i+k)%SIZE] > LOC[k-1]){//左に動かねばらならない?(右に動くと周回で1つ左の空白を抜く)

								MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];
							}
						}

						LOC[k] = work[FROM][k]+MOVE[k];
					}
				}

				if(FLG && abs(MOVE[SIZE-1])%2 == 1){

					FLG = false;
				}

				if(FLG){

					for(ll k = 0; k < SIZE; k++){

						tmp_sum += abs(MOVE[k])/2;
					}
					ans = min(ans,tmp_sum);
				}
			}
		}
	}

	if(ans == HUGE_NUM){

		printf("-1\n");
	}else{

		printf("%lld\n",ans);
	}

	return 0;
}
accuracy: 0.037037037037037035
time_ms: 130
memory_kb: 3404
final_rank: 19

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 3005

enum Type{
	FROM,
	TO,
};

ll N,M;
ll SIZE;
bool check_from[NUM],check_to[NUM];
vector<ll> V[2],work[2];
ll MOVE[NUM],ADD[NUM],LOC[NUM];


int main(){

	scanf("%lld %lld",&N,&M);

	for(ll i = 0; i < N; i++){

		check_from[i] = false;
		check_to[i] = false;
	}

	ll tmp;

	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_from[tmp] = true;
	}
	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_to[tmp] = true;
	}

	if(M == N){

		printf("0\n");
		return 0;
	}

	for(ll i = 0; i < N; i++){
		if(!check_from[i]){

			V[FROM].push_back(i);
		}
		if(!check_to[i]){

			V[TO].push_back(i);
		}
	}

	bool FLG = false;
	SIZE = N-M;

	for(ll i = 0; i < SIZE; i++){

		if(abs(V[FROM][(i+1)%SIZE]-V[FROM][i]+N)%N >= 3){

			FLG = true;
			break;
		}
	}

	if(!FLG && N-M > 1){

		for(ll i = 0; i < SIZE; i++){
			if(V[FROM][i] != V[TO][i]){

				printf("-1\n");
				return 0;
			}
		}

		printf("0\n");
		return 0;
	}

	ll ans = HUGE_NUM;

	for(ll i = 0; i < SIZE; i++){
		for(int loop = 0; loop < 2; loop++){

			if(loop == 0){

				work[FROM] = V[FROM];
				work[TO] = V[TO];
			}else{

				work[FROM] = V[TO];
				work[TO] = V[FROM];
			}

			/*FROM→TOへ右に動く*/

			if(work[TO][i] >= work[FROM][0]){

				MOVE[0] = work[TO][i]-work[FROM][0]; //どれだけ動くか

			}else{ //work[TO][i] < work[FROM][0]

				MOVE[0] = work[TO][i]-work[FROM][0]+N;
			}

			ADD[0] = MOVE[0];
			LOC[0] = ADD[0]+work[FROM][0]; //周回を考慮した位置

			FLG = true;

			//少なくとも1つ奇数動があるか調べる
			for(ll k = 1; k < SIZE; k++){
				if(abs(MOVE[k-1])%2 == 1){

					FLG = false;
					break;
				}

				if(LOC[k-1] >= work[FROM][k]){ //1つ左の移動先が、自分より右に位置する場合

					ADD[k] = LOC[k-1]-work[FROM][k]+1; //少なくとも、1つ左の移動先よりも,1つ右に位置できる分動く必要あり

					tmp = (ADD[k]+work[FROM][k])%N;
					MOVE[k] = ADD[k]+abs(work[TO][(i+k)%SIZE]-tmp+N)%N;

					LOC[k] = work[FROM][k]+MOVE[k];

				}else{ //1つ左の移動先が、自分より左に位置する場合


					if(work[TO][(i+k)%SIZE] >= work[FROM][k]){ //移動先が自分より右

						MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];

					}else{ //移動先が自分より左

						MOVE[k] = (work[TO][(i+k)%SIZE]-work[FROM][k]+N)%N;

						if(work[TO][(i+k)%SIZE] > LOC[k-1]){//左に動かねばらならない?

							MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];
						}
					}

					LOC[k] = work[FROM][k]+MOVE[k];
				}
			}

			if(FLG && abs(MOVE[SIZE-1])%2 == 1){

				FLG = false;
			}

			ll tmp_sum = 0;

			if(FLG){ //奇数動なし

				for(ll k = 0; k < SIZE; k++){

					tmp_sum += abs(MOVE[k])/2;
				}
				ans = min(ans,tmp_sum);

			}else{ //奇数動あり

				if(N%2 == 0)continue;

				//Nが奇数ならもう1周してみる
				for(ll k = 0; k < SIZE; k++){

					work[TO][k] += N;
				}

				FLG = true;

				MOVE[0] = work[TO][i]-work[FROM][0];

				//少なくとも1つ奇数動があるか調べる
				for(ll k = 1; k < SIZE; k++){
					if(abs(MOVE[k-1])%2 == 1){

						FLG = false;
						break;
					}

					if(LOC[k-1] >= work[FROM][k]){ //1つ左の移動先が、自分より右に位置する場合

						ADD[k] = LOC[k-1]-work[FROM][k]+1; //少なくとも、1つ左の移動先よりも,1つ右に位置できる分動く必要あり

						tmp = (ADD[k]+work[FROM][k])%N;
						MOVE[k] = ADD[k]+abs(work[TO][(i+k)%SIZE]-tmp+N)%N;

						LOC[k] = work[FROM][k]+MOVE[k];

					}else{ //1つ左の移動先が、自分より左に位置する場合


						if(work[TO][(i+k)%SIZE] >= work[FROM][k]){ //移動先が自分より右

							MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];

						}else{ //移動先が自分より左

							MOVE[k] = (work[TO][(i+k)%SIZE]-work[FROM][k]+N)%N;

							if(work[TO][(i+k)%SIZE] > LOC[k-1]){//左に動かねばらならない?

								MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];
							}
						}

						LOC[k] = work[FROM][k]+MOVE[k];
					}
				}

				if(FLG && abs(MOVE[SIZE-1])%2 == 1){

					FLG = false;
				}

				if(FLG){

					for(ll k = 0; k < SIZE; k++){

						tmp_sum += abs(MOVE[k])/2;
					}
					ans = min(ans,tmp_sum);
				}
			}
		}
	}

	if(ans == HUGE_NUM){

		printf("-1\n");
	}else{

		printf("%lld\n",ans);
	}

	return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 3480
final_rank: 3

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 3005

enum Type{
	FROM,
	TO,
};

ll N,M;
ll SIZE;
bool check_from[NUM],check_to[NUM];
vector<ll> V[2],work[2];
ll MOVE[NUM],ADD[NUM],LOC[NUM];


int main(){

	scanf("%lld %lld",&N,&M);

	for(ll i = 0; i < N; i++){

		check_from[i] = false;
		check_to[i] = false;
	}

	ll tmp;

	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_from[tmp] = true;
	}
	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_to[tmp] = true;
	}

	if(M == N){

		printf("0\n");
		return 0;
	}

	for(ll i = 0; i < N; i++){
		if(!check_from[i]){

			V[FROM].push_back(i);
		}
		if(!check_to[i]){

			V[TO].push_back(i);
		}
	}

	bool FLG = false;
	SIZE = N-M;

	for(ll i = 0; i < SIZE; i++){

		if(abs(V[FROM][(i+1)%SIZE]-V[FROM][i]+N)%N >= 3){

			FLG = true;
			break;
		}
	}

	if(!FLG && N-M > 1){

		for(ll i = 0; i < SIZE; i++){
			if(V[FROM][i] != V[TO][i]){

				printf("-1\n");
				return 0;
			}
		}

		printf("0\n");
		return 0;
	}

	ll ans = HUGE_NUM;

	for(ll i = 0; i < SIZE; i++){
		for(int loop = 0; loop < 2; loop++){

			if(loop == 0){

				work[FROM] = V[FROM];
				work[TO] = V[TO];
			}else{

				work[FROM] = V[TO];
				work[TO] = V[FROM];
			}

			/*FROM→TOへ右に動く*/

			if(work[TO][i] >= work[FROM][0]){

				MOVE[0] = work[TO][i]-work[FROM][0]; //どれだけ動くか

			}else{ //work[TO][i] < work[FROM][0]

				MOVE[0] = work[TO][i]-work[FROM][0]+N;
			}

			ADD[0] = MOVE[0];
			LOC[0] = ADD[0]+work[FROM][0]; //周回を考慮した位置

			FLG = true;

			//少なくとも1つ奇数動があるか調べる
			for(ll k = 1; k < SIZE; k++){
				if(abs(MOVE[k-1])%2 == 1){

					FLG = false;
					break;
				}

				if(LOC[k-1] >= work[FROM][k]){ //1つ左の移動先が、自分より右に位置する場合

					ADD[k] = LOC[k-1]-work[FROM][k]+1; //少なくとも、1つ左の移動先よりも,1つ右に位置できる分動く必要あり

					tmp = (ADD[k]+work[FROM][k])%N;
					MOVE[k] = ADD[k]+abs(work[TO][(i+k)%SIZE]-tmp+N)%N;

					LOC[k] = work[FROM][k]+MOVE[k];

				}else{ //1つ左の移動先が、自分より左に位置する場合


					if(work[TO][(i+k)%SIZE] >= work[FROM][k]){ //移動先が自分より右

						MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];

					}else{ //移動先が自分より左

						MOVE[k] = (work[TO][(i+k)%SIZE]-work[FROM][k]+N)%N;

						if((work[TO][(i+k)%SIZE] > LOC[k-1] && MOVE[k] > work[FROM][k]-work[TO][(i+k)%SIZE])){ //左に動いた方が低コスト

							MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];
						}
					}

					LOC[k] = work[FROM][k]+MOVE[k];
				}
			}

			if(FLG && abs(MOVE[SIZE-1])%2 == 1){

				FLG = false;
			}

			ll tmp_sum = 0;

			if(FLG){ //奇数動なし

				for(ll k = 0; k < SIZE; k++){

					tmp_sum += abs(MOVE[k])/2;
				}
				ans = min(ans,tmp_sum);

			}else{ //奇数動あり

				if(N%2 == 0)continue;

				//Nが奇数ならもう1周してみる
				for(ll k = 0; k < SIZE; k++){

					work[TO][k] += N;
				}

				FLG = true;

				MOVE[0] = work[TO][i]-work[FROM][0];

				//少なくとも1つ奇数動があるか調べる
				for(ll k = 1; k < SIZE; k++){
					if(abs(MOVE[k-1])%2 == 1){

						FLG = false;
						break;
					}

					if(LOC[k-1] >= work[FROM][k]){ //1つ左の移動先が、自分より右に位置する場合

						ADD[k] = LOC[k-1]-work[FROM][k]+1; //少なくとも、1つ左の移動先よりも,1つ右に位置できる分動く必要あり

						tmp = (ADD[k]+work[FROM][k])%N;
						MOVE[k] = ADD[k]+abs(work[TO][(i+k)%SIZE]-tmp+N)%N;

						LOC[k] = work[FROM][k]+MOVE[k];

					}else{ //1つ左の移動先が、自分より左に位置する場合


						if(work[TO][(i+k)%SIZE] >= work[FROM][k]){ //移動先が自分より右

							MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];

						}else{ //移動先が自分より左

							MOVE[k] = (work[TO][(i+k)%SIZE]-work[FROM][k]+N)%N;

							if(work[TO][(i+k)%SIZE] > LOC[k-1] && MOVE[k] > work[FROM][k]-work[TO][(i+k)%SIZE]){ //左に動いた方が低コスト

								MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];
							}
						}

						LOC[k] = work[FROM][k]+MOVE[k];
					}
				}

				if(FLG && abs(MOVE[SIZE-1])%2 == 1){

					FLG = false;
				}

				if(FLG){

					for(ll k = 0; k < SIZE; k++){

						tmp_sum += abs(MOVE[k])/2;
					}
					ans = min(ans,tmp_sum);
				}
			}
		}
	}

	if(ans == HUGE_NUM){

		printf("-1\n");
	}else{

		printf("%lld\n",ans);
	}

	return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 3500
final_rank: 6

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 3005

enum Type{
	FROM,
	TO,
};

ll N,M;
ll SIZE;
bool check_from[NUM],check_to[NUM];
vector<ll> V[2],work[2];
ll MOVE[NUM],ADD[NUM],LOC[NUM];


int main(){

	scanf("%lld %lld",&N,&M);

	for(ll i = 0; i < N; i++){

		check_from[i] = false;
		check_to[i] = false;
	}

	ll tmp;

	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_from[tmp] = true;
	}
	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_to[tmp] = true;
	}

	if(M == N){

		printf("0\n");
		return 0;
	}

	for(ll i = 0; i < N; i++){
		if(!check_from[i]){

			V[FROM].push_back(i);
		}
		if(!check_to[i]){

			V[TO].push_back(i);
		}
	}

	bool FLG = false;
	SIZE = N-M;

	for(ll i = 0; i < SIZE; i++){

		if(abs(V[FROM][(i+1)%SIZE]-V[FROM][i]+N)%N >= 3){

			FLG = true;
			break;
		}
	}

	if(!FLG && N-M > 1){

		for(ll i = 0; i < SIZE; i++){
			if(V[FROM][i] != V[TO][i]){

				printf("-1\n");
				return 0;
			}
		}

		printf("0\n");
		return 0;
	}

	ll ans = HUGE_NUM;

	for(ll i = 0; i < SIZE; i++){
		for(int loop = 0; loop < 2; loop++){

			if(loop == 0){

				work[FROM] = V[FROM];
				work[TO] = V[TO];
			}else{

				work[FROM] = V[TO];
				work[TO] = V[FROM];
			}

			/*FROM→TOへ右に動く*/

			if(work[TO][i] >= work[FROM][0]){

				MOVE[0] = work[TO][i]-work[FROM][0]; //どれだけ動くか

			}else{ //work[TO][i] < work[FROM][0]

				MOVE[0] = work[TO][i]-work[FROM][0]+N;
			}

			ADD[0] = MOVE[0];
			LOC[0] = ADD[0]+work[FROM][0]; //周回を考慮した位置

			FLG = true;

			//少なくとも1つ奇数動があるか調べる
			for(ll k = 1; k < SIZE; k++){
				if(abs(MOVE[k-1])%2 == 1){

					FLG = false;
					break;
				}

				if(LOC[k-1] >= work[FROM][k]){ //1つ左の移動先が、自分より右に位置する場合

					ADD[k] = LOC[k-1]-work[FROM][k]+1; //少なくとも、1つ左の移動先よりも,1つ右に位置できる分動く必要あり

					tmp = (ADD[k]+work[FROM][k])%N;
					MOVE[k] = ADD[k]+abs(work[TO][(i+k)%SIZE]-tmp+N)%N;

					LOC[k] = work[FROM][k]+MOVE[k];

				}else{ //1つ左の移動先が、自分より左に位置する場合


					if(work[TO][(i+k)%SIZE] >= work[FROM][k]){ //移動先が自分より右

						MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];

					}else{ //移動先が自分より左

						MOVE[k] = (work[TO][(i+k)%SIZE]-work[FROM][k]+N)%N;

						if(MOVE[k]%2 == 1 ||
								(work[TO][(i+k)%SIZE] > LOC[k-1] && MOVE[k] > work[FROM][k]-work[TO][(i+k)%SIZE])){ //左に動いた方が低コスト

							MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];
						}
					}

					LOC[k] = work[FROM][k]+MOVE[k];
				}
			}

			if(FLG && abs(MOVE[SIZE-1])%2 == 1){

				FLG = false;
			}

			ll tmp_sum = 0;

			if(FLG){ //奇数動なし

				for(ll k = 0; k < SIZE; k++){

					tmp_sum += abs(MOVE[k])/2;
				}
				ans = min(ans,tmp_sum);

			}else{ //奇数動あり

				if(N%2 == 0)continue;

				//Nが奇数ならもう1周してみる
				for(ll k = 0; k < SIZE; k++){

					work[TO][k] += N;
				}

				FLG = true;

				MOVE[0] = work[TO][i]-work[FROM][0];

				//少なくとも1つ奇数動があるか調べる
				for(ll k = 1; k < SIZE; k++){
					if(abs(MOVE[k-1])%2 == 1){

						FLG = false;
						break;
					}

					if(LOC[k-1] >= work[FROM][k]){ //1つ左の移動先が、自分より右に位置する場合

						ADD[k] = LOC[k-1]-work[FROM][k]+1; //少なくとも、1つ左の移動先よりも,1つ右に位置できる分動く必要あり

						tmp = (ADD[k]+work[FROM][k])%N;
						MOVE[k] = ADD[k]+abs(work[TO][(i+k)%SIZE]-tmp+N)%N;

						LOC[k] = work[FROM][k]+MOVE[k];

					}else{ //1つ左の移動先が、自分より左に位置する場合


						if(work[TO][(i+k)%SIZE] >= work[FROM][k]){ //移動先が自分より右

							MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];

						}else{ //移動先が自分より左

							MOVE[k] = (work[TO][(i+k)%SIZE]-work[FROM][k]+N)%N;

							if(MOVE[k]%2 == 1 ||
									(work[TO][(i+k)%SIZE] > LOC[k-1] && MOVE[k] > work[FROM][k]-work[TO][(i+k)%SIZE])){ //左に動いた方が低コスト

								MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];
							}
						}

						LOC[k] = work[FROM][k]+MOVE[k];
					}
				}

				if(FLG && abs(MOVE[SIZE-1])%2 == 1){

					FLG = false;
				}

				if(FLG){

					for(ll k = 0; k < SIZE; k++){

						tmp_sum += abs(MOVE[k])/2;
					}
					ans = min(ans,tmp_sum);
				}
			}
		}
	}

	if(ans == HUGE_NUM){

		printf("-1\n");
	}else{

		printf("%lld\n",ans);
	}

	return 0;
}
accuracy: 0.16666666666666666
time_ms: 140
memory_kb: 3456
final_rank: 18

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 3005

enum Type{
	FROM,
	TO,
};

ll N,M;
ll SIZE;
bool check_from[NUM],check_to[NUM];
vector<ll> V[2],work[2];
ll MOVE[NUM],ADD[NUM],LOC[NUM];


int main(){

	scanf("%lld %lld",&N,&M);

	for(ll i = 0; i < N; i++){

		check_from[i] = false;
		check_to[i] = false;
	}

	ll tmp;

	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_from[tmp] = true;
	}
	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_to[tmp] = true;
	}

	if(M == N){

		printf("0\n");
		return 0;
	}

	for(ll i = 0; i < N; i++){
		if(!check_from[i]){

			V[FROM].push_back(i);
		}
		if(!check_to[i]){

			V[TO].push_back(i);
		}
	}

	bool FLG = false;
	SIZE = N-M;

	for(ll i = 0; i < SIZE; i++){

		if(abs(V[FROM][(i+1)%SIZE]-V[FROM][i]+N)%N >= 3){

			FLG = true;
			break;
		}
	}

	if(!FLG && N-M > 1){

		for(ll i = 0; i < SIZE; i++){
			if(V[FROM][i] != V[TO][i]){

				printf("-1\n");
				return 0;
			}
		}

		printf("0\n");
		return 0;
	}

	ll ans = HUGE_NUM;

	for(ll i = 0; i < SIZE; i++){
		for(int loop = 0; loop < 2; loop++){

			if(loop == 0){

				work[FROM] = V[FROM];
				work[TO] = V[TO];
			}else{

				work[FROM] = V[TO];
				work[TO] = V[FROM];
			}

			/*FROM→TOへ右に動く*/

			if(work[TO][i] >= work[FROM][0]){

				MOVE[0] = work[TO][i]-work[FROM][0]; //どれだけ動くか

			}else{ //work[TO][i] < work[FROM][0]

				MOVE[0] = work[TO][i]-work[FROM][0]+N;
			}

			ADD[0] = MOVE[0];
			LOC[0] = ADD[0]+work[FROM][0]; //周回を考慮した位置

			FLG = true;

			//少なくとも1つ奇数動があるか調べる
			for(ll k = 1; k < SIZE; k++){
				if(abs(MOVE[k-1])%2 == 1){

					FLG = false;
					break;
				}

				if(LOC[k-1] >= work[FROM][k]){ //1つ左の移動先が、自分より右に位置する場合

					ADD[k] = LOC[k-1]-work[FROM][k]+1; //少なくとも、1つ左の移動先よりも,1つ右に位置できる分動く必要あり

					tmp = (ADD[k]+work[FROM][k])%N;
					MOVE[k] = ADD[k]+abs(work[TO][(i+k)%SIZE]-tmp+N)%N;

					LOC[k] = work[FROM][k]+MOVE[k];

				}else{ //1つ左の移動先が、自分より左に位置する場合


					if(work[TO][(i+k)%SIZE] >= work[FROM][k]){ //移動先が自分より右

						MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];

					}else{ //移動先が自分より左

						MOVE[k] = (work[TO][(i+k)%SIZE]-work[FROM][k]+N)%N;

						if(work[TO][(i+k)%SIZE] > LOC[k-1] && MOVE[k] > work[FROM][k]-work[TO][(i+k)%SIZE]){ //左に動いた方が低コスト

							MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k]; //動きが右でも左でも、その偶奇は一致するはず
						}
					}

					LOC[k] = work[FROM][k]+MOVE[k];
				}
			}

			if(FLG && abs(MOVE[SIZE-1])%2 == 1){

				FLG = false;
			}

			ll tmp_sum = 0;

			if(FLG){ //奇数動なし

				for(ll k = 0; k < SIZE; k++){

					tmp_sum += abs(MOVE[k])/2;
				}
				ans = min(ans,tmp_sum);

			}else{ //奇数動あり

				if(N%2 == 0)continue;

				//Nが奇数ならもう1周してみる
				for(ll k = 0; k < SIZE; k++){

					work[TO][k] += N;
				}

				FLG = true;

				MOVE[0] = work[TO][i]-work[FROM][0];

				//少なくとも1つ奇数動があるか調べる
				for(ll k = 1; k < SIZE; k++){
					if(abs(MOVE[k-1])%2 == 1){

						FLG = false;
						break;
					}

					if(LOC[k-1] >= work[FROM][k]){ //1つ左の移動先が、自分より右に位置する場合

						ADD[k] = LOC[k-1]-work[FROM][k]+1; //少なくとも、1つ左の移動先よりも,1つ右に位置できる分動く必要あり

						tmp = (ADD[k]+work[FROM][k])%N;
						MOVE[k] = ADD[k]+abs(work[TO][(i+k)%SIZE]-tmp+N)%N;

						LOC[k] = work[FROM][k]+MOVE[k];

					}else{ //1つ左の移動先が、自分より左に位置する場合


						if(work[TO][(i+k)%SIZE] >= work[FROM][k]){ //移動先が自分より右

							MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];

						}else{ //移動先が自分より左

							MOVE[k] = (work[TO][(i+k)%SIZE]-work[FROM][k]+N)%N;

							if(work[TO][(i+k)%SIZE] > LOC[k-1] && MOVE[k] > work[FROM][k]-work[TO][(i+k)%SIZE]){ //左に動いた方が低コスト

								MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];
							}
						}

						LOC[k] = work[FROM][k]+MOVE[k];
					}
				}

				if(FLG && abs(MOVE[SIZE-1])%2 == 1){

					FLG = false;
				}

				if(FLG){

					for(ll k = 0; k < SIZE; k++){

						tmp_sum += abs(MOVE[k])/2;
					}
					ans = min(ans,tmp_sum);
				}
			}
		}
	}

	if(ans == HUGE_NUM){

		printf("-1\n");
	}else{

		printf("%lld\n",ans);
	}

	return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 3480
final_rank: 3

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 3005

enum Type{
	FROM,
	TO,
};

ll N,M;
ll SIZE;
bool check_from[NUM],check_to[NUM];
vector<ll> V[2],work[2];
ll MOVE[NUM],ADD[NUM],LOC[NUM];


int main(){

	scanf("%lld %lld",&N,&M);

	for(ll i = 0; i < N; i++){

		check_from[i] = false;
		check_to[i] = false;
	}

	ll tmp;

	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_from[tmp] = true;
	}
	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_to[tmp] = true;
	}

	if(M == N){

		printf("0\n");
		return 0;
	}

	for(ll i = 0; i < N; i++){
		if(!check_from[i]){

			V[FROM].push_back(i);
		}
		if(!check_to[i]){

			V[TO].push_back(i);
		}
	}

	bool FLG = false;
	SIZE = N-M;

	for(ll i = 0; i < SIZE; i++){

		if(abs(V[FROM][(i+1)%SIZE]-V[FROM][i]+N)%N >= 3){

			FLG = true;
			break;
		}
	}

	if(!FLG && N-M > 1){

		for(ll i = 0; i < SIZE; i++){
			if(V[FROM][i] != V[TO][i]){

				printf("-1\n");
				return 0;
			}
		}

		printf("0\n");
		return 0;
	}

	ll ans = HUGE_NUM;

	for(ll i = 0; i < SIZE; i++){
		for(int loop = 0; loop < 2; loop++){

			if(loop == 0){

				work[FROM] = V[FROM];
				work[TO] = V[TO];
			}else{

				work[FROM] = V[TO];
				work[TO] = V[FROM];
			}

			/*FROM→TOへ右に動く*/

			if(work[TO][i] >= work[FROM][0]){

				MOVE[0] = work[TO][i]-work[FROM][0]; //どれだけ動くか

			}else{ //work[TO][i] < work[FROM][0]

				MOVE[0] = work[TO][i]-work[FROM][0]+N;
			}

			ADD[0] = MOVE[0];
			LOC[0] = ADD[0]+work[FROM][0]; //周回を考慮した位置

			FLG = true;

			//少なくとも1つ奇数動があるか調べる
			for(ll k = 1; k < SIZE; k++){
				if(abs(MOVE[k-1])%2 == 1){

					FLG = false;
					break;
				}

				if(LOC[k-1] >= work[FROM][k]){ //1つ左の移動先が、自分より右に位置する場合

					ADD[k] = LOC[k-1]-work[FROM][k]+1; //少なくとも、1つ左の移動先よりも,1つ右に位置できる分動く必要あり

					tmp = (ADD[k]+work[FROM][k])%N;
					MOVE[k] = ADD[k]+abs(work[TO][(i+k)%SIZE]-tmp+N)%N;

					LOC[k] = work[FROM][k]+MOVE[k];

				}else{ //1つ左の移動先が、自分より左に位置する場合


					if(work[TO][(i+k)%SIZE] >= work[FROM][k]){ //移動先が自分より右

						MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];

					}else{ //移動先が自分より左

						MOVE[k] = (work[TO][(i+k)%SIZE]-work[FROM][k]+N)%N;

						if(work[TO][(i+k)%SIZE] > LOC[k-1] && MOVE[k] > work[FROM][k]-work[TO][(i+k)%SIZE]){ //左に動いた方が低コスト

							MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];
						}
					}

					LOC[k] = work[FROM][k]+MOVE[k];
				}
			}

			if(FLG && MOVE[SIZE-1]%2 == 1){

				FLG = false;
			}

			ll tmp_sum = 0;

			if(FLG){ //奇数動なし

				for(ll k = 0; k < SIZE; k++){

					tmp_sum += abs(MOVE[k])/2;
				}
				ans = min(ans,tmp_sum);

			}else{ //奇数動あり

				if(N%2 == 0)continue;

				//Nが奇数ならもう1周してみる
				for(ll k = 0; k < SIZE; k++){

					work[TO][k] += N;
				}

				FLG = true;

				MOVE[0] = work[TO][i]-work[FROM][0];

				//少なくとも1つ奇数動があるか調べる
				for(ll k = 1; k < SIZE; k++){
					if(abs(MOVE[k-1])%2 == 1){

						FLG = false;
						break;
					}

					if(LOC[k-1] >= work[FROM][k]){ //1つ左の移動先が、自分より右に位置する場合

						ADD[k] = LOC[k-1]-work[FROM][k]+1; //少なくとも、1つ左の移動先よりも,1つ右に位置できる分動く必要あり

						tmp = (ADD[k]+work[FROM][k])%N;
						MOVE[k] = ADD[k]+abs(work[TO][(i+k)%SIZE]-tmp+N)%N;

						LOC[k] = work[FROM][k]+MOVE[k];

					}else{ //1つ左の移動先が、自分より左に位置する場合


						if(work[TO][(i+k)%SIZE] >= work[FROM][k]){ //移動先が自分より右

							MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];

						}else{ //移動先が自分より左

							MOVE[k] = (work[TO][(i+k)%SIZE]-work[FROM][k]+N)%N;

							if(work[TO][(i+k)%SIZE] > LOC[k-1] && MOVE[k] > work[FROM][k]-work[TO][(i+k)%SIZE]){ //左に動いた方が低コスト

								MOVE[k] = work[TO][(i+k)%SIZE]-work[FROM][k];
							}
						}

						LOC[k] = work[FROM][k]+MOVE[k];
					}
				}

				if(FLG && MOVE[SIZE-1]%2 == 1){

					FLG = false;
				}

				if(FLG){

					for(ll k = 0; k < SIZE; k++){

						tmp_sum += abs(MOVE[k])/2;
					}
					ans = min(ans,tmp_sum);
				}
			}
		}
	}

	if(ans == HUGE_NUM){

		printf("-1\n");
	}else{

		printf("%lld\n",ans);
	}

	return 0;
}
accuracy: 0.25925925925925924
time_ms: 140
memory_kb: 3500
final_rank: 17

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


enum Type{
	FROM,
	TO,
};

ll N,M;
ll SIZE;
bool check_from[3005],check_to[3005];
vector<ll> V[2],work[2];
ll MOVE[3005];


int main(){

	scanf("%lld %lld",&N,&M);

	for(ll i = 0; i < N; i++){

		check_from[i] = false;
		check_to[i] = false;
	}

	ll tmp;

	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_from[tmp] = true;
	}
	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_to[tmp] = true;
	}

	if(M == N){

		printf("0\n");
		return 0;
	}

	for(ll i = 0; i < N; i++){
		if(!check_from[i]){

			V[FROM].push_back(i);
		}
		if(!check_to[i]){

			V[TO].push_back(i);
		}
	}

	bool FLG = false;
	SIZE = N-M;

	for(ll i = 0; i < SIZE; i++){

		if(abs(V[FROM][(i+1)%SIZE]-V[FROM][i]+N)%N >= 3){

			FLG = true;
			break;
		}
	}

	if(!FLG && N-M > 1){

		for(ll i = 0; i < SIZE; i++){
			if(V[FROM][i] != V[TO][i]){

				printf("-1\n");
				return 0;
			}
		}

		printf("0\n");
		return 0;
	}

	ll ans = HUGE_NUM;

	for(ll i = 0; i < SIZE; i++){
		for(int loop = 0; loop < 2; loop++){

			if(loop == 0){

				work[FROM] = V[FROM];
				work[TO] = V[TO];
			}else{

				work[FROM] = V[TO];
				work[TO] = V[FROM];
			}

			/*FROM→TOへ右に動く*/

			if(work[TO][i] >= work[FROM][0]){

				MOVE[0] = work[TO][i]-work[FROM][0];

			}else{ //work[TO][i] < work[FROM][0]

				MOVE[0] = work[TO][i]-work[FROM][0]+N;
			}

			FLG = true;

			//少なくとも1つ奇数動があるか調べる
			for(ll k = 1; k < SIZE; k++){
				if(MOVE[k-1]%2 == 1){

					FLG = false;
					break;
				}
				ll pre = (work[FROM][k-1]+MOVE[k-1]); //1つ左の空白位置

				if(pre >= work[FROM][k]){

					MOVE[k] = pre-work[FROM][k]+1; //少なくとも、1つ左よりも右に位置できる分動く必要あり

				}else{

					MOVE[k] = 0;
				}

				tmp = (MOVE[k]+work[FROM][k])%N;

				MOVE[k] += abs(work[TO][(i+k)%SIZE]-tmp+N)%N;
			}
			if(FLG && MOVE[SIZE-1]%2 == 1){

				FLG = false;
			}

			ll tmp_sum = 0;

			if(FLG){ //奇数動なし

				for(ll k = 0; k < SIZE; k++){

					tmp_sum += MOVE[k]/2;
				}
				ans = min(ans,tmp_sum);

			}else{ //奇数動あり

				if(N%2 == 0)continue;

				//Nが奇数ならもう1周してみる
				for(ll k = 0; k < SIZE; k++){

					work[TO][k] += N;
				}

				FLG = true;

				MOVE[0] = work[TO][i]-work[FROM][0];

				//少なくとも1つ奇数動があるか調べる
				for(ll k = 1; k < SIZE; k++){
					if(MOVE[k-1]%2 == 1){

						FLG = false;
						break;
					}
					ll pre = (work[FROM][k-1]+MOVE[k-1]); //1つ左の空白位置

					if(pre >= work[FROM][k]){

						MOVE[k] = pre-work[FROM][k]+1; //少なくとも、1つ左よりも右に位置できる分動く必要あり

					}else{

						MOVE[k] = 0;
					}


					tmp = (MOVE[k]+work[FROM][k])%N;

					MOVE[k] += abs(work[TO][(i+k)%SIZE]-tmp+N)%N;
				}
				if(FLG && MOVE[SIZE-1]%2 == 1){

					FLG = false;
				}

				if(FLG){

					for(ll k = 0; k < SIZE; k++){

						tmp_sum += MOVE[k]/2;
					}
					ans = min(ans,tmp_sum);
				}
			}
		}
	}

	if(ans == HUGE_NUM){

		printf("-1\n");
	}else{

		printf("%lld\n",ans);
	}

	return 0;
}
accuracy: 0.5185185185185185
time_ms: 260
memory_kb: 3440
final_rank: 8

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


enum Type{
	FROM,
	TO,
};

ll N,M;
ll SIZE;
bool check_from[3005],check_to[3005];
vector<ll> V[2],work[2];
ll MOVE[3005];


int main(){

	scanf("%lld %lld",&N,&M);

	for(ll i = 0; i < N; i++){

		check_from[i] = false;
		check_to[i] = false;
	}

	ll tmp;

	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_from[tmp] = true;
	}
	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_to[tmp] = true;
	}

	if(M == N){

		printf("0\n");
		return 0;
	}

	for(ll i = 0; i < N; i++){
		if(!check_from[i]){

			V[FROM].push_back(i);
		}
		if(!check_to[i]){

			V[TO].push_back(i);
		}
	}

	bool FLG = false;
	SIZE = N-M;

	for(ll i = 0; i < SIZE; i++){

		if(abs(V[FROM][(i+1)%SIZE]-V[FROM][i]+N)%N >= 3){

			FLG = true;
			break;
		}
	}

	if(!FLG && N-M > 1){

		for(ll i = 0; i < SIZE; i++){
			if(V[FROM][i] != V[TO][i]){

				printf("-1\n");
				return 0;
			}
		}

		printf("0\n");
		return 0;
	}

	ll ans = HUGE_NUM;

	for(ll i = 0; i < SIZE; i++){
		for(int loop = 0; loop < 2; loop++){

			if(loop == 0){

				work[FROM] = V[FROM];
				work[TO] = V[TO];
			}else{

				work[FROM] = V[TO];
				work[TO] = V[FROM];
			}

			/*FROM→TOへ右に動く*/

			if(work[TO][i] >= work[FROM][0]){

				MOVE[0] = work[TO][i]-work[FROM][0];

			}else{ //work[TO][i] < work[FROM][0]

				MOVE[0] = work[TO][i]-work[FROM][0]+N;
			}

			FLG = true;

			//少なくとも1つ奇数動があるか調べる
			for(ll k = 1; k < SIZE; k++){
				if(MOVE[k-1]%2 == 1){

					FLG = false;
					break;
				}
				ll pre = (work[FROM][k-1]+MOVE[k-1]); //1つ左の空白位置

				if(pre >= work[FROM][k]){

					MOVE[k] = MOVE[k-1]; //少なくとも、[1つ左と同じだけ]動く必要あり

				}else{

					MOVE[k] = 0;
				}

				tmp = (MOVE[k]+work[FROM][k])%N;

				MOVE[k] += abs(work[TO][(i+k)%SIZE]-tmp+N)%N;
			}
			if(FLG && MOVE[SIZE-1]%2 == 1){

				FLG = false;
			}

			ll tmp_sum = 0;

			if(FLG){ //奇数動なし

				for(ll k = 0; k < SIZE; k++){

					tmp_sum += MOVE[k]/2;
				}
				ans = min(ans,tmp_sum);

			}else{ //奇数動あり

				if(N%2 == 0)continue;

				//Nが奇数ならもう1周してみる
				for(ll k = 0; k < SIZE; k++){

					work[TO][k] += N;
				}

				FLG = true;

				MOVE[0] = work[TO][i]-work[FROM][0];

				//少なくとも1つ奇数動があるか調べる
				for(ll k = 1; k < SIZE; k++){
					if(MOVE[k-1]%2 == 1){

						FLG = false;
						break;
					}
					ll pre = (work[FROM][k-1]+MOVE[k-1]); //1つ左の空白位置

					if(pre >= work[FROM][k]){

						MOVE[k] = MOVE[k-1]; //少なくとも、[1つ左と同じだけ]動く必要あり

					}else{

						MOVE[k] = 0;
					}


					tmp = (MOVE[k]+work[FROM][k])%N;

					MOVE[k] += abs(work[TO][(i+k)%SIZE]-tmp+N)%N;
				}
				if(FLG && MOVE[SIZE-1]%2 == 1){

					FLG = false;
				}

				if(FLG){

					for(ll k = 0; k < SIZE; k++){

						tmp_sum += MOVE[k]/2;
					}
					ans = min(ans,tmp_sum);
				}
			}
		}
	}

	if(ans == HUGE_NUM){

		printf("-1\n");
	}else{

		printf("%lld\n",ans);
	}

	return 0;
}
accuracy: 0.3888888888888889
time_ms: 250
memory_kb: 3420
final_rank: 9

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


enum Type{
	FROM,
	TO,
};

ll N,M;
ll SIZE;
bool check_from[3005],check_to[3005];
vector<ll> V[2],work[2];
ll MOVE[3005];


int main(){

	scanf("%lld %lld",&N,&M);

	for(ll i = 0; i < N; i++){

		check_from[i] = false;
		check_to[i] = false;
	}

	ll tmp;

	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_from[tmp] = true;
	}
	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_to[tmp] = true;
	}

	for(ll i = 0; i < N; i++){
		if(!check_from[i]){

			V[FROM].push_back(i);
		}
		if(!check_to[i]){

			V[TO].push_back(i);
		}
	}

	bool FLG = false;
	SIZE = N-M;

	for(ll i = 0; i < SIZE; i++){

		if(abs(V[FROM][(i+1)%SIZE]-V[FROM][i]+N)%N >= 3){

			FLG = true;
			break;
		}
	}

	if(!FLG && N-M > 1){

		for(ll i = 0; i < SIZE; i++){
			if(V[FROM][i] != V[TO][i]){

				printf("-1\n");
				return 0;
			}
		}

		printf("0\n");
		return 0;
	}

	ll ans = HUGE_NUM;

	for(ll i = 0; i < SIZE; i++){
		for(int loop = 0; loop < 2; loop++){

			if(loop == 0){

				work[FROM] = V[FROM];
				work[TO] = V[TO];
			}else{

				work[FROM] = V[TO];
				work[TO] = V[FROM];
			}

			/*FROM→TOへ右に動く*/

			if(work[TO][i] >= work[FROM][0]){

				MOVE[0] = work[TO][i]-work[FROM][0];

			}else{ //work[TO][i] < work[FROM][0]

				MOVE[0] = work[TO][i]-work[FROM][0]+N;
			}

			FLG = true;

			//少なくとも1つ奇数動があるか調べる
			for(ll k = 1; k < SIZE; k++){
				if(MOVE[k-1]%2 == 1){

					FLG = false;
					break;
				}
				ll pre = (work[FROM][k-1]+MOVE[k-1]); //1つ左の空白位置

				if(pre >= work[FROM][k]){

					MOVE[k] = MOVE[k-1]; //少なくとも、[1つ左と同じだけ]動く必要あり

				}else{

					MOVE[k] = 0;
				}

				tmp = (MOVE[k]+work[FROM][k])%N;

				MOVE[k] += abs(work[TO][(i+k)%SIZE]-tmp+N)%N;
			}
			if(FLG && MOVE[SIZE-1]%2 == 1){

				FLG = false;
			}

			ll tmp_sum = 0;

			if(FLG){ //奇数動なし

				for(ll k = 0; k < SIZE; k++){

					tmp_sum += MOVE[k]/2;
				}
				ans = min(ans,tmp_sum);

			}else{ //奇数動あり

				if(N%2 == 0)continue;

				//Nが奇数ならもう1周してみる
				for(ll k = 0; k < SIZE; k++){

					work[TO][k] += N;
				}

				FLG = true;

				MOVE[0] = work[TO][i]-work[FROM][0];

				//少なくとも1つ奇数動があるか調べる
				for(ll k = 1; k < SIZE; k++){
					if(MOVE[k-1]%2 == 1){

						FLG = false;
						break;
					}
					ll pre = (work[FROM][k-1]+MOVE[k-1]); //1つ左の空白位置

					if(pre >= work[FROM][k]){

						MOVE[k] = MOVE[k-1]; //少なくとも、[1つ左と同じだけ]動く必要あり

					}else{

						MOVE[k] = 0;
					}


					tmp = (MOVE[k]+work[FROM][k])%N;

					MOVE[k] += abs(work[TO][(i+k)%SIZE]-tmp+N)%N;
				}
				if(FLG && MOVE[SIZE-1]%2 == 1){

					FLG = false;
				}

				if(FLG){

					for(ll k = 0; k < SIZE; k++){

						tmp_sum += MOVE[k]/2;
					}
					ans = min(ans,tmp_sum);
				}
			}
		}
	}

	if(ans == HUGE_NUM){

		printf("-1\n");
	}else{

		printf("%lld\n",ans);
	}

	return 0;
}
accuracy: 0.35185185185185186
time_ms: 250
memory_kb: 3452
final_rank: 10

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


enum Type{
	FROM,
	TO,
};

ll N,M;
ll SIZE;
bool check_from[3005],check_to[3005];
vector<ll> V[2],work[2];
ll MOVE[3005];


int main(){

	scanf("%lld %lld",&N,&M);

	for(ll i = 0; i < N; i++){

		check_from[i] = false;
		check_to[i] = false;
	}

	ll tmp;

	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_from[tmp] = true;
	}
	for(ll i = 0; i < M; i++){

		scanf("%lld",&tmp);
		tmp--;

		check_to[tmp] = true;
	}

	for(ll i = 0; i < N; i++){
		if(!check_from[i]){

			V[FROM].push_back(i);
		}
		if(!check_to[i]){

			V[TO].push_back(i);
		}
	}

	bool FLG = false;
	SIZE = N-M;

	for(ll i = 0; i < SIZE; i++){

		if(abs(V[FROM][(i+1)%SIZE]-V[FROM][i]+N)%N >= 3){

			FLG = true;
			break;
		}
	}

	if(!FLG){

		for(ll i = 0; i < SIZE; i++){
			if(V[FROM][i] != V[TO][i]){

				printf("-1\n");
				return 0;
			}
		}

		printf("0\n");
		return 0;
	}

	ll ans = HUGE_NUM;

	for(ll i = 0; i < SIZE; i++){
		for(int loop = 0; loop < 2; loop++){

			if(loop == 0){

				work[FROM] = V[FROM];
				work[TO] = V[TO];
			}else{

				work[FROM] = V[TO];
				work[TO] = V[FROM];
			}

			/*FROM→TOへ右に動く*/

			if(work[TO][i] >= work[FROM][0]){

				MOVE[0] = work[TO][i]-work[FROM][0];

			}else{ //work[TO][i] < work[FROM][0]

				MOVE[0] = work[TO][i]-work[FROM][0]+N;
			}

			FLG = true;

			//少なくとも1つ奇数動があるか調べる
			for(ll k = 1; k < SIZE; k++){
				if(MOVE[k-1]%2 == 1){

					FLG = false;
					break;
				}
				ll pre = (work[FROM][k-1]+MOVE[k-1]); //1つ左の空白位置

				if(pre >= work[FROM][k]){

					MOVE[k] = MOVE[k-1]; //少なくとも、[1つ左と同じだけ]動く必要あり

				}else{

					MOVE[k] = 0;
				}

				tmp = (MOVE[k]+work[FROM][k])%N;

				MOVE[k] += abs(work[TO][(i+k)%SIZE]-tmp+N)%N;
			}
			if(FLG && MOVE[SIZE-1]%2 == 1){

				FLG = false;
			}

			ll tmp_sum = 0;

			if(FLG){ //奇数動なし

				for(ll k = 0; k < SIZE; k++){

					tmp_sum += MOVE[k]/2;
				}
				ans = min(ans,tmp_sum);

			}else{ //奇数動あり

				if(N%2 == 0)continue;

				//Nが奇数ならもう1周してみる
				for(ll k = 0; k < SIZE; k++){

					work[TO][k] += N;
				}

				FLG = true;

				MOVE[0] = work[TO][i]-work[FROM][0];

				//少なくとも1つ奇数動があるか調べる
				for(ll k = 1; k < SIZE; k++){
					if(MOVE[k-1]%2 == 1){

						FLG = false;
						break;
					}
					ll pre = (work[FROM][k-1]+MOVE[k-1]); //1つ左の空白位置

					if(pre >= work[FROM][k]){

						MOVE[k] = MOVE[k-1]; //少なくとも、[1つ左と同じだけ]動く必要あり

					}else{

						MOVE[k] = 0;
					}


					tmp = (MOVE[k]+work[FROM][k])%N;

					MOVE[k] += abs(work[TO][(i+k)%SIZE]-tmp+N)%N;
				}
				if(FLG && MOVE[SIZE-1]%2 == 1){

					FLG = false;
				}

				if(FLG){

					for(ll k = 0; k < SIZE; k++){

						tmp_sum += MOVE[k]/2;
					}
					ans = min(ans,tmp_sum);
				}
			}
		}
	}

	if(ans == HUGE_NUM){

		printf("-1\n");
	}else{

		printf("%lld\n",ans);
	}

	return 0;
}
accuracy: 0.3333333333333333
time_ms: 250
memory_kb: 3432
final_rank: 11