#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
    int n;
    scanf("%d", &n);
    getchar();
    char *s = (char *)malloc((n+1)*sizeof(char));
    int *stack = (int *)malloc((n+1)*sizeof(int));
    if(s==NULL || stack==NULL){
        printf("Error: Memory allocation failed.\n");
        retyrn 1;
    }
    int i;
    int isValid = 1;
    int actualLength = 0;
    for(i=0; i<n; i++){
        int c=getchar();
        if(c=='\n' || c==EOF){
            break;
        }
        if(c!='(' && c!=')'){
            isValid = 0;
        }
        s[i] = (char)c;
        actualLength++;
    }
    s[actualLength] = '\0';
    if(actualLength!=n || isValid == 0){
        printf("Invalid input\n");
        free(s);
        free(stack);
        return 0;
    }
    int maxLength = 0;
    int top = -1;
    stack[++top] = -1;
    for(i=0; i<n; i++){
        if(s[i]=='('){
            stack[++top]=i;
        }else{
            if(top>0){
                top--;
                int currentLength = i-stack[top];
                if(currentLength>maxLength){
                    maxLength = currentLength;
                }
            }else{
                stack[top]=i;
            }
        }
    }
    free(stack);
    return 0;
}