#include <bits/stdc++.h>
using namespace std;
const long long N = (long long)(1e6) + 322;
const long long inf = (long long)(1e18) + 322;
const long long INF = (long long)(1e18) + 322;
const long long mod = (long long)(1e9) + 7;
long long n, ans, bal[N];
string s;
signed main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> s;
  s = '@' + s;
  for (long long i = 1; i <= n; ++i) {
    bal[i] = bal[i - 1] + (s[i] == '(' ? 1 : -1);
  }
  if (bal[n] != 0) {
    cout << -1;
    return 0;
  }
  long long last = 0;
  for (long long i = 1; i <= n; ++i) {
    if (bal[i] == 0) {
      if (bal[i - 1] < 0) {
        ans += (i - last);
      }
      last = i;
    }
  }
  cout << ans;
  return 0;
}
