#include <bits/stdc++.h>
long long R = 7 + 1e9, NUMTESTCASE;
const int NN = 10 + 1e6;
const double pi = acos(-1.0);
int di[4] = {1, 0, -1, 0}, dj[4] = {0, 1, 0, -1},
    DI[8] = {1, 1, 0, -1, -1, -1, 0, 1}, DJ[8] = {0, 1, 1, 1, 0, -1, -1, -1};
using namespace std;
long long n, Cnt1, Cnt2, Ans;
string s;
int main() {
  cin >> n >> s;
  for (int i = (0); i <= (n - 1); ++i)
    if (s[i] == '(')
      Cnt1++;
    else
      Cnt2++;
  if (Cnt1 != Cnt2) return cout << -1, 0;
  Cnt1 = Cnt2 = 0;
  for (int i = (0); i <= (n - 1); ++i) {
    string Draft = "";
    if (s[i] == '(')
      Cnt1++;
    else
      Cnt2++;
    Draft += s[i];
    if (Cnt2 > Cnt1) {
      Ans++;
      int Flag = 0;
      while (Cnt2 > Cnt1) {
        i++;
        if (s[i] == '(')
          Cnt1++;
        else
          Cnt2++;
        Ans++;
      }
      Cnt1 = Cnt2 = 0;
    }
  }
  cout << Ans;
  return 0;
}
