#include <bits/stdc++.h>
using namespace std;
long long dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
long long dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
const long double pi = 3.141592653589;
const long long mod = 1000000007;
const long long maxn = 1e5 + 5;
template <typename T>
void printv(const vector<T>& v) {
  for (auto i : v) cout << i << ' ';
  cout << '\n';
}
template <typename T>
void printv(const vector<pair<T, T>>& v) {
  for (auto p : v) cout << "(" << p.first << "," << p.second << "),";
  cout << '\n';
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  long long n;
  string s;
  cin >> n >> s;
  long long open = 0, closed = 0;
  for (auto i : s) {
    if (i == '(')
      open++;
    else
      closed++;
  }
  if ((n & 1) || (open != closed))
    cout << -1 << '\n';
  else {
    long long ans = 0;
    long long ck = 0;
    for (auto i : s) {
      if (i == '(')
        ck++;
      else {
        ck--;
        if (ck < 0) {
          ans += 2;
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
