#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
template <class T>
bool ckmin(T &a, const T &b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 5000003;
void solve() {
  int n;
  cin >> n;
  int c1 = 0, c0 = 0;
  string second;
  cin >> second;
  for (int i = 0; i < n; i++) {
    if (second[i] == '(') {
      c1++;
    } else {
      c0++;
    }
  }
  if (c1 != c0) {
    cout << -1 << nl;
    return;
  }
  int cnt = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (second[i] == '(') {
      cnt++;
    } else {
      cnt--;
    }
    if (cnt < 0 || (cnt == 0 && second[i] == '(')) ans++;
  }
  cout << ans << nl;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  solve();
}
