#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
using ll = long long;
const int INF = (1<<30)-1;
const long long LINF = (1LL<<62)-1;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  map<int, int> mp;
  rep(i, s.size()) mp[s[i]-'0']++;
  rep(i, 1000) {
    if (i%8)
      continue;
    vector<int> num(10);
    int tmp = i;
    rep(_, min(3, (int)s.size())) {
      num[tmp%10]++;
      tmp /= 10;
    }
    bool flag = true;
    rep(i, 10) {
      if (mp[i] < num[i])
        flag = false;
    }
    if (flag) {
      cout << "Yes\n";
      return 0;
    }
  }
  cout << "No\n";
  return 0;
}
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <map>
#include <unordered_map>
using namespace std;

#define debug(x) cout << #x << '=' << x << '\n'
#define rep(i, b, e) for (int i = b; i <= e; i++)

const int N = 212345;

int n, a[N], b[N], c[N];
unordered_map<int, queue<int>> mp;

struct BIT {
    int c[N], size;
    inline int lowbit(const int &x) {
        return x & (-x);
    }
    inline void add(int loc, const int &val) {
        for (; loc <= size; loc += lowbit(loc)) {
            c[loc] += val;
        }
    }
    inline int query(int loc) {
        int ret = 0;
        for (; loc; loc -= lowbit(loc)) {
            ret += c[loc];
        }
        return ret;
    }
} Bit;

int main(){
    cin >> n;
    rep (i, 1, n) {
        scanf("%d", a + i);
        a[i] += i;
        mp[a[i]].push(i);
    }
    rep (i, 1, n) {
        scanf("%d", b + i);
        b[i] += i;
        if (mp[b[i]].size() == 0) {
            puts("-1");
            return 0;
        }
        c[i] = mp[b[i]].front();
        mp[b[i]].pop();
    }
    //rep (i, 1, n) {
    //    debug(c[i]);
    //}
    Bit.size = n;
    long long ans = 0;
    for (int i = n; i > 0; i--) {
        ans += Bit.query(c[i] - 1);
        Bit.add(c[i], 1);
    }
    cout << ans;
    return 0;
}
