#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
bool isprime(long long int n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
const int mod = 1e9 + 7;
const long long int INF = 1e18;
const int num = 1e5 + 1;
void solve(int io) {
  long long int n;
  cin >> n;
  string s;
  cin >> s;
  if (n % 2) {
    cout << -1 << "\n";
  } else {
    int cnt = 0;
    for (char c : s) {
      if (c == ')') cnt++;
    }
    if (cnt * 2 != n) {
      cout << -1 << "\n";
    } else {
      int ans = 0;
      int cc = 0, oo = 0;
      for (char c : s) {
        if (c == '(')
          oo++;
        else
          cc++;
        if (cc == oo) {
          if (c == '(') {
            ans += oo + cc;
          }
          oo = 0, cc = 0;
        }
      }
      cout << ans << "\n";
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int test = 1;
  for (long long int i = 0; i < test; i++) {
    solve(i + 1);
  }
}
