#include <bits/stdc++.h>

#define rep(i, a, b) for (int i = a; i < b; i++)
#define all(v) (v).begin(), (v).end()
#define inp(t, ...) \
  t __VA_ARGS__;    \
  in(__VA_ARGS__)
#define inv(t, v, n) \
  vector<t> v(n);    \
  for (t & num : v)  \
    cin >> num;
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;
inline void in(void) {
  return;
}
template <typename First, typename... Rest>
void in(First& first, Rest&... rest) {
  cin >> first;
  in(rest...);
  return;
}
inline void out(void) {
  return;
}
template <typename First, typename... Rest>
void out(First first, Rest... rest) {
  cout << first << " ";
  out(rest...);
  return;
}
int main() {
  inp(ll, n);
  ll ans = 0;
  rep(i, 1, 6){
    ll m = n - pow(1000, i);
    if (m >= 0) ans += (m + 1);
  }
  out(ans);
}
#include <bits/stdc++.h>
using namespace std;

class InversionCount {
public:
    InversionCount(vector<int64_t> target) : target_(target) {}
    int64_t count(int64_t left = 0, int64_t right = -1) {
        if (right < 0) {
            //rはtarget_.size()で初期化
            right = target_.size();
        }
        if (right <= left + 1) {
            return 0;
        }
        int64_t mid = (left + right) / 2;
        int64_t result = 0;
        //左半分を数える
        result += count(left, mid);

        //右半分を数える
        result += count(mid, right);

        //左右またぐ数を数える
        result += merge(left, mid, right);

        return result;
    }
private:
    int64_t merge(int64_t left, int64_t mid, int64_t right) {
        vector<int64_t> l, r;
        for (int64_t i = left; i < mid; i++) {
            l.push_back(target_[i]);
        }
        for (int64_t i = mid; i < right; i++) {
            r.push_back(target_[i]);
        }
        //番兵
        l.push_back(LLONG_MAX);
        r.push_back(LLONG_MAX);

        int64_t left_index = 0;
        int64_t right_index = 0;
        int64_t result = 0;
        for (int64_t i = left; i < right; i++) {
            if (l[left_index] <= r[right_index]) {
                target_[i] = l[left_index];
                left_index++;
            } else {
                target_[i] = r[right_index];
                right_index++;
                result += ((mid - left) - left_index);
            }
        }
        return result;
    }

    vector<int64_t> target_;
};

int main() {
    int64_t N;
    cin >> N;
    vector<int64_t> A(N), B(N);
    for (int64_t& i : A) {
        cin >> i;
    }
    for (int64_t& i : B) {
        cin >> i;
    }

    const int64_t sum_A = accumulate(A.begin(), A.end(), (int64_t)0);
    const int64_t sum_B = accumulate(B.begin(), B.end(), (int64_t)0);

    if (sum_A != sum_B) {
        cout << -1 << endl;
        return 0;
    }

    const int64_t odd_num_A = count_if(A.begin(), A.end(), [](int64_t x) { return x % 2 == 1; });
    const int64_t odd_num_B = count_if(B.begin(), B.end(), [](int64_t x) { return x % 2 == 1; });
    const int64_t even_num_A = count_if(A.begin(), A.end(), [](int64_t x) { return x % 2 == 0; });
    const int64_t even_num_B = count_if(B.begin(), B.end(), [](int64_t x) { return x % 2 == 0; });

    if (odd_num_A % 2 != odd_num_B % 2 || even_num_A % 2 != even_num_B % 2) {
        cout << -1 << endl;
        return 0;
    }

    multiset<pair<int64_t, int64_t>> st;
    for (int64_t i = 0; i < N; i++) {
        A[i] += i;
        B[i] += i;

        st.insert(make_pair(A[i], i));
    }

    vector<int64_t> gt_index(N);
    for (int64_t i = 0; i < N; i++) {
        auto itr = st.lower_bound(make_pair(B[i], -1));
        if (itr == st.end() || itr->first != B[i]) {
            cout << -1 << endl;
            return 0;
        }
        gt_index[itr->second] = i;
        st.erase(itr);
    }

    InversionCount ic(gt_index);
    cout << ic.count() << endl;
}