#include <bits/stdc++.h>
#include <numeric>
using namespace std;
#define ll long long
#define mod107 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)
using Graph = vector<vector<int>>;
#define grepq priority_queue<double, std::vector<double>, std::greater<double>>
#define all(v) v.begin(), v.end()
#define PI acos(-1)
const ll INF = 10010001010;
const int inf = 1000034000;
typedef vector<ll> vecl;
typedef vector<int> veci;
typedef tuple<string, int, int> TUP;

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

int main()
{
    int N;
    cin >> N;
    ll ans = 0;
    vector<int> A(N);
    vector<int> B(N);
    vector<int> C(N);
    for (int i = 0; i < N;i++)
    {
        cin >> A[i];
    }
    for (int i = 0; i < N; i++)
    {
        cin >> B[i];
    }
    for (int i = 0; i < N; i++)
    {
        cin >> C[i];
    }
    sort(all(A));
    sort(all(B));
    sort(all(C));
    for (int i = 0; i < N;i++)
    {
        auto itA = lower_bound(A.begin(),A.end(),B[i]);
        auto itC = upper_bound(C.begin(), C.end(),B[i]);
        ll a = itA - A.begin();
        ll b = C.end() - itC;
        //cout << a <<" " << b << endl;
        ans += a * b;
    }
    cout << ans << endl;
}

#include <iostream>
#include <vector>
#include <cassert>
#include <map>

using ll = long long;

#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

int const nmax = 200000;
int v[1 + nmax];
bool valid(int n) {
  if(0 < v[1])
    return 0;
  for(int i = 2; i <= n; i++) 
    if(v[i - 1] + 1 < v[i])
      return 0;
  return 1;
}
int frec[1 + nmax];

int main() {
  int n;
  std::cin >> n;
  for(int i = 1;i <= n; i++)
    std::cin >> v[i];
  if(valid(n) == 0)
    std::cout << -1;
  else {
    ll result = 0;
    for(int i = n; 1 <= i; i--) {
      if(frec[i - v[i]] == 0) {
        result += v[i];
        frec[i - v[i]] = 1;
      }
    }
    std::cout << result;
  }
  return 0;
}
