import java.awt.Point;
import java.awt.geom.Line2D;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.security.GuardedObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.io.InputStream;
import java.math.BigInteger;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */

public class Main {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;

        OutputStream outputStream = System.out;
        FastScanner in;
        PrintWriter out;

           in=new FastScanner(System.in);
            out=new PrintWriter(System.out);
        //in=new FastScanner(new FileInputStream(new File("C://Users//KANDARP//Desktop//coding_contest_creation (1).txt")));

        TaskC solver = new TaskC();
        long var=System.currentTimeMillis();
        solver.solve(1, in, out);
        /*if(System.getProperty("ONLINE_JUDGE")==null){
            out.println("["+(double)(System.currentTimeMillis()-var)/1000+"]");
        }*/
        out.close();
    }
}
class Pair {

    long x,y,index;
    long  r1,r2;
    HashMap<Double,Long> map=new HashMap<>();
    Pair(long x,long y,int index){
        this.x=x;
        this.y=y;
        this.index=index;
    }
    public String toString(){
        return this.x+" "+this.y+" ";
    }

}
class tupple{
    long x,y,z;
    ArrayList<String> list=new ArrayList<>();
    tupple(long x,long y,long z){
        this.x=x;
        this.y=y;
        this.z=z;
    }
    tupple(tupple cl){
        this.x=cl.x;
        this.y=cl.y;
        this.z=cl.z;
        this.list=cl.list;
    }
}
class Edge implements  Comparable<Edge>{
    int u;
    int v;
    int time;
    long cost;
    long wcost;
    public Edge(int u,int v,long cost,long wcost){
        this.u=u;
        this.v=v;
        this.cost=cost;
        this.wcost=wcost;
        time=Integer.MAX_VALUE;
    }
    public int other(int x){
        if(u==x)return v;
        return u;
    }
    @Override
    public int compareTo(Edge o) {
        // TODO Auto-generated method stub
        return Long.compare(cost, o.cost);
    }
}
class Ss{
    Integer a[]=new Integer[3];
    Ss(Integer a[]){
        this.a=a;

    }
    public int hashCode(){
        return a[0].hashCode()+a[1].hashCode()+a[2].hashCode();
    }
    public boolean equals(Object o){
        Ss x=(Ss)o;
        for(int i=0;i<3;i++){
            if(x.a[i]!=this.a[i]){
                return false;
            }
        }
        return true;

    }
}
class queary{
    int type;
    int l;
    int r;
    int index;
    queary(int type,int l,int r){
        this.type=type;
        this.l=l;
        this.r=r;
    }
}
class boat implements Comparable<boat>{
    int capacity;
    int index;
    boat(int capacity,int index)
    {
        this.capacity=capacity;
        this.index=index;
    }
    @Override
    public int compareTo(boat o) {
        // TODO Auto-generated method stub
        return this.capacity-o.capacity;
    }
}
class angle{
    double x,y,angle;
    int index;
    angle(int x,int y,double angle){
        this.x=x;
        this.y=y;
        this.angle=angle;
    }
}
class TaskC {
    static long mod=1000000007;
//    SegTree tree;
    //    int min[];
 /*   static int prime_len=1000010;
    static int prime[]=new int[prime_len];
    static long n_prime[]=new long[prime_len];
    static ArrayList<Integer> primes=new ArrayList<>();
    static{
        for(int i=2;i<=Math.sqrt(prime.length);i++){
            for(int j=i*i;j<prime.length;j+=i){

                prime[j]=i;

            }
        }
        for(int i=2;i<prime.length;i++){
            n_prime[i]=n_prime[i-1];
            if(prime[i]==0){
               n_prime[i]++;
            }

        }
    //  System.out.println("end");

    //  prime[0]=true;
    //  prime[1]=1;
    }
    /*
     *  long pp[]=new long[10000001];
            pp[0]=0;
            pp[1]=1;
            for(int i=2;i<pp.length;i++){
                if(prime[i]!=0){
                    int gcd=(int)greatestCommonDivisor(i/prime[i], prime[i]);
                    pp[i]=(pp[i/prime[i]]*pp[prime[i]]*gcd)/pp[(int)gcd];
                }
                else
                    pp[i]=i-1;
            }

}
     * */
    class Rmq {

		  int[] logTable;
		  int[][] rmq;
		  int[] a;

		  public Rmq(int[] a) {
		    this.a = a;
		    int n = a.length;

		    logTable = new int[n + 1];
		    for (int i = 2; i <= n; i++)
		      logTable[i] = logTable[i >> 1] + 1;

		    rmq = new int[logTable[n] + 1][n];

		    for (int i = 0; i < n; ++i)
		      rmq[0][i] = i;

		    for (int k = 1; (1 << k) < n; ++k) {
		      for (int i = 0; i + (1 << k) <= n; i++) {
		        int x = rmq[k - 1][i];
		        int y = rmq[k - 1][i + (1 << k - 1)];
		        rmq[k][i] = a[x] <= a[y] ? x : y;
		      }
		    }
		  }

		  public int minPos(int i, int j) {
		    int k = logTable[j - i];
		    int x = rmq[k][i];
		    int y = rmq[k][j - (1 << k) + 1];
		    return a[x] <= a[y] ? a[x] : a[y];
		  }

	}

   public void solve(int testNumber, FastScanner in, PrintWriter out) throws IOException {

	   String s=in.readString();
	   int x=0;
	   long ans=0;
	   for(int i=0;i<s.length()-1;i++){
		  x=Integer.parseInt(s.charAt(i)+""+s.charAt(i+1)+"");
		//  out.println(x);
		  if(x%4==0){
			  ans+=i+1;
		  }
		  if((s.charAt(i)-'0')%4==0)
		  {
			  ans++;
		  }
	   }
	   if((s.charAt(s.length()-1)-'0')%4==0){
		   ans++;
	   }
	   			   out.println(ans);

   }

   static long greatestCommonDivisor (long m, long n){
       long x;
       long y;
       while(m%n != 0){
           x = n;
           y = m%n;
           m = x;
           n = y;
       }
       return n;
   }

 }

    /*
     * 	   long res[]=new long[m];
    	   int cl=1;
    	   int cr=0;
    	   for(int i=0;i<m;i++){
    		   int l=(int)q[i].x;
    		   int r=(int)q[i].y;
    		   while(cl>l){
    			   cl--;
    			   ans+=add(cl);

    		   }
    		   while(cl<l){
    			   ans-=remove(cl);
    			   cl++;

    		   }
    		   while(cr>r){
    			   ans-=remove(cr);
    			   cr--;
    		   }
    		   while(cr<r){
    			   cr++;
    			   ans+=add(cr);
    		   }
    		   res[(int)q[i].index]=ans;
    	   }
    	   for(int i=0;i<m;i++)
    		   out.println(res[i]);
     */

	   	 class SegmentTree2D {
          public  long max(int[][] t, int x1, int y1, int x2, int y2) {
            int n = t.length >> 1;
            x1 += n;
            x2 += n;
            int m = t[0].length >> 1;
            y1 += m;
            y2 += m;
            long res = 0;
            for (int lx = x1, rx = x2; lx <= rx; lx = (lx + 1) >> 1, rx = (rx - 1) >> 1)
              for (int ly = y1, ry = y2; ly <= ry; ly = (ly + 1) >> 1, ry = (ry - 1) >> 1) {
                if ((lx & 1) != 0 && (ly & 1) != 0) res = (res+ t[lx][ly]);
                if ((lx & 1) != 0 && (ry & 1) == 0) res = (res+ t[lx][ry]);
                if ((rx & 1) == 0 && (ly & 1) != 0) res =(res+t[rx][ly]);
                if ((rx & 1) == 0 && (ry & 1) == 0) res = (res+ t[rx][ry]);
              }
            return res;
          }

          public  void add(int[][] t, int x, int y, int value) {
            x += t.length >> 1;
            y += t[0].length >> 1;
            t[x][y] += value;
            for (int tx = x; tx > 0; tx >>= 1)
              for (int ty = y; ty > 0; ty >>= 1) {
                if (tx > 1) t[tx >> 1][ty] = (t[tx][ty]+t[tx ^ 1][ty]);
                if (ty > 1) t[tx][ty >> 1] = (t[tx][ty]+ t[tx][ty ^ 1]);
              }

//          for (int ty = y; ty > 1; ty >>= 1)
//            t[x][ty >> 1] = Math.max(t[x][ty], t[x][ty ^ 1]);
//          for (int tx = x; tx > 1; tx >>= 1)
//            for (int ty = y; ty > 0; ty >>= 1)
//              t[tx >> 1][ty] = Math.max(t[tx][ty], t[tx ^ 1][ty]);

//          for (int lx=x; lx> 0; lx >>= 1) {
//            if (lx > 1)
//              t[lx >> 1][y] = Math.max(t[lx][y], t[lx ^ 1][y]);
//            for (int ly = y; ly > 1; ly >>= 1)
//              t[lx][ly >> 1] = Math.max(t[lx][ly], t[lx][ly ^ 1]);
//          }
          }
    }

     class SegmentTree {

          // Modify the following 5 methods to implement your custom operations on the tree.
          // This example implements Add/Max operations. Operations like Add/Sum, Set/Max can also be implemented.
          long modifyOperation(long x, long y) {
            return  y;
          }

          // query (or combine) operation
          long queryOperation(long leftValue, long rightValue) {
            return (leftValue| rightValue);
          }

          long deltaEffectOnSegment(long delta, long segmentLength) {
            // Here you must write a fast equivalent of following slow code:
            // int result = delta;
            // for (int i = 1; i < segmentLength; i++) result = queryOperation(result, delta);
            // return result;
            return delta;
          }

          int getNeutralDelta() {
            return 0;
          }

          int getInitValue() {
            return 0;
          }

          // generic code
          int n;
          long[] value;
          long[] delta; // delta[i] affects value[i], delta[2*i+1] and delta[2*i+2]

          long joinValueWithDelta(long value, long delta) {
            if (delta == getNeutralDelta()) return value;
            return modifyOperation(value, delta);
          }

          long joinDeltas(long delta1, long delta2) {
            if (delta1 == getNeutralDelta()) return delta2;
            if (delta2 == getNeutralDelta()) return delta1;
            return modifyOperation(delta1, delta2);
          }

          void pushDelta(int root, int left, int right) {
            value[root] = joinValueWithDelta(value[root], deltaEffectOnSegment(delta[root], right - left + 1));
            delta[2 * root + 1] = joinDeltas(delta[2 * root + 1], delta[root]);
            delta[2 * root + 2] = joinDeltas(delta[2 * root + 2], delta[root]);
            delta[root] = getNeutralDelta();
          }

          public SegmentTree(int n) {
            this.n = n;
            value = new long[4 * n];
            delta = new long[4 * n];
            init(0, 0, n - 1);
          }

          void init(int root, int left, int right) {
            if (left == right) {
              value[root] = getInitValue();
              delta[root] = getNeutralDelta();
            } else {
              int mid = (left + right) >> 1;
              init(2 * root + 1, left, mid);
              init(2 * root + 2, mid + 1, right);
              value[root] = queryOperation(value[2 * root + 1], value[2 * root + 2]);
              delta[root] = getNeutralDelta();
            }
          }

          public long query(int from, int to) {
            return query(from, to, 0, 0, n - 1);
          }

          long query(int from, int to, int root, int left, int right) {
            if (from == left && to == right)
              return joinValueWithDelta(value[root], deltaEffectOnSegment(delta[root], right - left + 1));
            pushDelta(root, left, right);
            int mid = (left + right) >> 1;
            if (from <= mid && to > mid)
              return queryOperation(
                  query(from, Math.min(to, mid), root * 2 + 1, left, mid),
                  query(Math.max(from, mid + 1), to, root * 2 + 2, mid + 1, right));
            else if (from <= mid)
              return query(from, Math.min(to, mid), root * 2 + 1, left, mid);
            else if (to > mid)
              return query(Math.max(from, mid + 1), to, root * 2 + 2, mid + 1, right);
            else
              throw new RuntimeException("Incorrect query from " + from + " to " + to);
          }

          public void modify(int from, int to, long delta) {
            modify(from, to, delta, 0, 0, n - 1);
          }

          void modify(int from, int to, long delta, int root, int left, int right) {
            if (from == left && to == right) {
              this.delta[root] = joinDeltas(this.delta[root], delta);
              return;
            }
            pushDelta(root, left, right);
            int mid = (left + right) >> 1;
            if (from <= mid)
              modify(from, Math.min(to, mid), delta, 2 * root + 1, left, mid);
            if (to > mid)
              modify(Math.max(from, mid + 1), to, delta, 2 * root + 2, mid + 1, right);
            value[root] = queryOperation(
                joinValueWithDelta(value[2 * root + 1], deltaEffectOnSegment(this.delta[2 * root + 1], mid - left + 1)),
                joinValueWithDelta(value[2 * root + 2], deltaEffectOnSegment(this.delta[2 * root + 2], right - mid)));
          }
    }

    class LcaSparseTable {

          int len;
          int[][] up;
          int[][] max;
          int[] tin;
          int[] tout;
          int time;
          int []lvel;

          void dfs(List<Integer>[] tree, int u, int p) {
            tin[u] = time++;
            lvel[u]=lvel[p]+1;
            up[0][u] = p;
            if(u!=p)
            //max[0][u]=weight(u,p);
            for (int i = 1; i < len; i++)
              {
                up[i][u] = up[i - 1][up[i - 1][u]];
                max[i][u]=Math.max(max[i-1][u],max[i-1][up[i-1][u]]);
              }
            for (int v : tree[u])
              if (v != p)
                dfs(tree, v, u);
            tout[u] = time++;
          }

          public LcaSparseTable(List<Integer>[] tree, int root) {
            int n = tree.length;
            len = 1;
            while ((1 << len) <= n) ++len;
            up = new int[len][n];
            max=new int[len][n];
            tin = new int[n];
            tout = new int[n];
            lvel=new int[n];
            lvel[root]=0;
            dfs(tree, root, root);
          }

          boolean isParent(int parent, int child) {
            return tin[parent] <= tin[child] && tout[child] <= tout[parent];
          }

          public int lca(int a, int b) {
            if (isParent(a, b))
              return a;
            if (isParent(b, a))
              return b;
            for (int i = len - 1; i >= 0; i--)
              if (!isParent(up[i][a], b))
                a = up[i][a];
            return up[0][a];
          }
          public long max(int a,int b){
              int lca=lca(a,b);
        //    System.out.println("LCA "+lca);
              long ans=0;
              int h=lvel[a]-lvel[lca];
            //  System.out.println("Height "+h);
              int index=0;
              while(h!=0){
                  if(h%2==1){
                      ans=Math.max(ans,max[index][a]);
                      a=up[index][a];
                  }
                  h/=2;
                  index++;
              }
              h=lvel[b]-lvel[lca];
             // System.out.println("Height "+h);

              index=0;
              while(h!=0){
                  if(h%2==1){
                      ans=Math.max(ans,max[index][b]);
                      b=up[index][b];
                  }
                  h/=2;
                  index++;
              }
              return ans;
          }

    static void dfs(List<Integer>[] graph, boolean[] used, List<Integer> res, int u,int parent,List<Integer> collection) {

        used[u] = true;
        Integer uu=new Integer(u);
        collection.add(uu);
        for (int v : graph[u]){
          if (!used[v]){
            dfs(graph, used, res, v,u,collection);

          }
          else if(collection.contains(v)){
              System.out.println("Impossible");
              System.exit(0);
          }
        }
        collection.remove(uu);
        res.add(u);
      }

      public static List<Integer> topologicalSort(List<Integer>[] graph) {
        int n = graph.length;
        boolean[] used = new boolean[n];
        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < n; i++)
          if (!used[i])
            dfs(graph, used, res, i,-1,new ArrayList<Integer>());
        Collections.reverse(res);
        return res;
      }

    static class pairs{
                String company,color;
                String type;
                boolean visit=false;
                pairs(String company,String color,String type){
                    this.company=company;
                    this.color=color;
                    this.type=type;
                }
            }

            static void dfs1(List<Integer>[] graph, boolean[] used, List<Integer> res, int u) {
            used[u] = true;
            for (int v : graph[u])
              if (!used[v])
                dfs1(graph, used, res, v);
            res.add(u);
          }

          public static List<Integer> topologicalSort1(List<Integer>[] graph) {
            int n = graph.length;
            boolean[] used = new boolean[n];
            List<Integer> res = new ArrayList<>();
            for (int i = n-1; i >0; i--)
              if (!used[i])
                dfs1(graph, used, res, i);
            Collections.reverse(res);
            return res;
          }

        int phi(int n) {
            int res = n;
            for (int i = 2; i * i <= n; ++i) {
                if (n % i == 0) {
                    while (n % i == 0) {
                        n /= i;
                    }
                    res -= res / i;
                }
            }
            if (n != 1) {
                res -= res / n;
            }
            return res;
        }
        long pow(long x,long y,long mod){
            if(y<=0){
                return 1;
            }
            if(y==1){
                return x%mod;
            }
            long temp=pow(x,y/2,mod);
            if(y%2==0){
                return (temp*temp)%mod;
            }
            else{
                return (((temp*temp)%mod)*x)%mod;
            }
        }

/*      long DP(int id,int mask){
            //System.out.println("hi"+dp[id][mask]+" "+id+" "+Integer.toBinaryString(mask));
            if(id==0 && mask==0){
                dp[0][mask]=1;
                return 1;
            }
            else if(id==0){
                dp[0][mask]=0;
                return 0;
            }
            if(dp[id][mask]!=-1){

                return dp[id][mask];
            }
            long ans=0;
            for(int i=0;i<n;i++){
                if((mask & (1<<i))!=0 && c[i][id]>=1){
                        ans+=DP(id-1,mask ^ (1 << i));
                        ans%=mod;
                }
            }
            ans+=DP(id-1,mask);
            ans%=mod;
            dp[id][mask]=ans;
            return ans;
        }*/
                long no_of_primes(long m,long n,long k){
                    long count=0,i,j;
                    int primes []=new int[(int)(n-m+2)];
                       if(m==1)    primes[0] = 1;
                        for(i=2; i<=Math.sqrt(n); i++)
                        {
                            j = (m/i);    j *= i;
                            if(j<m)
                                j+=i;
                            for(; j<=n; j+=i)
                             {
                                 if(j!=i)
                                    primes[(int)(j-m)] = 1;
                             }
                        }

                for(i=0; i<=n-m; i++)
                            if(primes[(int)i]==0 && (i-1)%k==0)
                                count++;
                return count;
                }
}
class SegTree {
    int n;
    long  t[];
    long mod=(long)(1000000007);
    SegTree(int n,long  t[]){
        this.n=n;
        this.t=t;
        build();
    }

    void build() {  // build the tree

        for (int i = n - 1; i > 0; --i)
            t[i]=(t[i<<1]|t[i<<1|1]);
    }

     void modify(int p, long value) {  // set value at position p
      for (t[p += n]+=value; p > 1; p >>= 1) t[p>>1] = (t[p]|t[p^1]);
    }

     long query(int l, int r) {  // sum on interval [l, r)
      long res=0;

      for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if ((l&1)!=0) res=Math.max(res,t[l++]);
            if ((r&1)!=0) res=Math.max(res,t[--r]);
          }
      return res;
    }

    //
//
//
}
class FastScanner
{

    private InputStream stream;
    private byte[] buf = new byte[8192];
    private int curChar;
    private int snumChars;
    private SpaceCharFilter filter;

    public FastScanner(InputStream stream) {
        this.stream = stream;
    }

    public int snext() {
        if (snumChars == -1)
            throw new InputMismatchException();
        if (curChar >= snumChars) {
            curChar = 0;
            try {
                snumChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (snumChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }
    public int nextInt() {
        int c = snext();
        while (isSpaceChar(c))
            c = snext();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = snext();
        }

        int res = 0;

        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = snext();
        } while (!isSpaceChar(c));

        return res * sgn;
    }

    public long nextLong() {
        int c = snext();
        while (isSpaceChar(c))
            c = snext();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = snext();
        }

        long res = 0;

        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = snext();
        } while (!isSpaceChar(c));

        return res * sgn;
    }

    public String readString() {
        int c = snext();
        while (isSpaceChar(c))
            c = snext();
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = snext();
        } while (!isSpaceChar(c));
        return res.toString();
    }

    public boolean isSpaceChar(int c) {
        if (filter != null)
            return filter.isSpaceChar(c);
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public interface SpaceCharFilter {
        public boolean isSpaceChar(int ch);
    }
}
 class UF {

    private int[] parent;  // parent[i] = parent of i
    private byte[] rank;   // rank[i] = rank of subtree rooted at i (never more than 31)
    private int count;     // number of components
    public UF(int N) {
        if (N < 0) throw new IllegalArgumentException();
        count = N;
        parent = new int[N];
        rank = new byte[N];
        for (int i = 0; i < N; i++) {
            parent[i] = i;
            rank[i] = 0;
        }
    }

    public int find(int p) {
        if (p < 0 || p >= parent.length) throw new IndexOutOfBoundsException();
        while (p != parent[p]) {
            parent[p] = parent[parent[p]];    // path compression by halving
            p = parent[p];
        }
        return p;
    }
    public int count() {
        return count;
    }

    public boolean connected(int p, int q) {
        return find(p) == find(q);
    }

    public boolean union(int p, int q) {
        int rootP = find(p);
        int rootQ = find(q);
        if (rootP == rootQ) return false;

        // make root of smaller rank point to root of larger rank
        if      (rank[rootP] < rank[rootQ]) parent[rootP] = rootQ;
        else if (rank[rootP] > rank[rootQ]) parent[rootQ] = rootP;
        else {
            parent[rootQ] = rootP;
            rank[rootP]++;
        }
        count--;
        return true;
    }

}
 class MyArrayList {

    private int[] myStore;
    private int actSize = 0;

    public MyArrayList(){
        myStore = new int[2];
    }

    public int get(int index){
        if(index < actSize)
            return myStore[index];
         else
            throw new ArrayIndexOutOfBoundsException();
    }

    public void add(int obj){
        if(myStore.length-actSize <= 1)
            increaseListSize();
        myStore[actSize++] = obj;
    }

    public int size(){
        return actSize;
    }

    private void increaseListSize(){
        myStore = Arrays.copyOf(myStore, myStore.length*2);
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class C628B {

	public static void main(String[] args)throws Exception {

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

		String s = in.readLine();

		long ans = 0;

		if(((int)(s.charAt(0) - 48)) % 4 == 0)
			ans ++;

		for(int i = 1;i < s.length();i ++){
			int n = Integer.valueOf(s.substring(i - 1 , i + 1));
			if(n % 4 == 0)
				ans += i;
			if(((int)(s.charAt(i) - 48)) % 4 == 0)
				ans ++;
		}

		System.out.println(ans);

	}
}


import java.util.Scanner;

public class B {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		StringBuffer buffer = new StringBuffer(scanner.nextLine());

		//
		long ans = 0;
		for(int i = 0; i < buffer.length(); i++) {
			if(buffer.charAt(i) == '8' || buffer.charAt(i) == '4' || buffer.charAt(i) == '0') {
				ans++;
			}

		}

		//
		for(int i = 1; i < buffer.length(); i++) {
			char b = buffer.charAt(i - 1);
			char a = buffer.charAt(i);
			int pair = Integer.valueOf("" + b + a);
			if(pair % 4 == 0) {
				ans += i;

			}

		}

		System.out.println(ans);

	}

}


import java.util.Scanner;

public class B {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		StringBuffer buffer = new StringBuffer(scanner.nextLine());

		// Count all 4s
		int ans = 0;
		for(int i = 0; i < buffer.length(); i++) {
			if(buffer.charAt(i) == '4' || buffer.charAt(i) == '0') {
				ans++;
			}

		}

		//
		for(int i = 1; i < buffer.length(); i++) {
			char b = buffer.charAt(i - 1);
			char a = buffer.charAt(i);
			int pair = Integer.valueOf("" + b + a);
			if(pair % 4 == 0) {
				ans += i;
			//	System.out.println("P: " + pair + " " + i);

			}

		}

		System.out.println(ans);

	}

}


import java.util.Scanner;

public class B {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		StringBuffer buffer = new StringBuffer(scanner.nextLine());

		// Count all 4s
		int ans = 0;
		for(int i = 0; i < buffer.length(); i++) {
			if(buffer.charAt(i) == '8' || buffer.charAt(i) == '4' || buffer.charAt(i) == '0') {
				ans++;
			}

		}

		//
		for(int i = 1; i < buffer.length(); i++) {
			char b = buffer.charAt(i - 1);
			char a = buffer.charAt(i);
			int pair = Integer.valueOf("" + b + a);
			if(pair % 4 == 0) {
				ans += i;
			//	System.out.println("P: " + pair + " " + i);

			}

		}

		System.out.println(ans);

	}

}


import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String n = in.next();
        int ans = 0;
        if (Long.parseLong(n.substring(n.length() - 2)) % 4 == 0) {
            ans++;
        } else {
            ans--;
        }
        System.out.println(ans+n.length());

//        for (int i = 0; i < n.length() - 2; i++) {
//            System.out.println("XXX=" + Long.parseLong(n.substring(i, i + 2)));
//            if (Long.parseLong(n.substring(i, i + 2)) % 4 == 0) {
//                ans++;
//            }
//            System.out.println("ans=" + ans);
//        }
//        System.out.println(ans);
    }

}


import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        StringBuilder out = new StringBuilder();

        String n = in.next();
        long x, ans = 0;

        for (int i = 0; i < n.length(); i++) {
            x = Long.parseLong(n.charAt(i) + "");
            if (x % 4 == 0) {
                ans++;
            }

        }
        for (int i = 0; i < n.length() - 1; i++) {
            x = Long.parseLong(n.substring(i, i + 2));
            if (x % 4 == 0) {
                ans += i + 1;
            }
        }
        System.out.println(ans);

    }

}


import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        StringBuilder out = new StringBuilder();

        String n = in.next();
        long ans = 0;

        long x = Long.parseLong(n);

//        if (x % 4 == 0) {
//            ans++;
//
//            System.out.println("x=" + x);
//        }
        for (int i = 0; i < n.length(); i++) {
            if ((long) n.charAt(i) % 4 == 0) {
                ans++;

                //System.out.println("AAA=" + n.charAt(i));
            }

        }

        for (int i = 0; i < n.length() - 1; i++) {
            if (Long.parseLong(n.substring(i, i + 2)) % 4 == 0) {
                ans += i + 1;
                // System.out.println("XXX="+Long.parseLong(n.substring(i, i + 2)));
            }

        }
        System.out.println(ans);

    }

}


import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String n = in.next();
        long ans = 0;
        if (n.length() > 1) {
            if (Long.parseLong(n.substring(n.length() - 2)) % 4 == 0) {
                ans++;
            } else {
                ans--;
            }
        }
        if (n.length() == 1 && n.equals("4")) {
            System.out.println(1);
        } else if (n.length() == 1 && !n.equals("4")) {
            System.out.println(0);
        } else {
            System.out.println(ans + n.length());
        }

    }

}


import java.io.*;
import java.util.StringTokenizer;

/**
 * Created by peacefrog on 2/19/16.
 * 10:13 PM
 */
public class Task_B {
	final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
	PrintWriter out;
	long timeBegin, timeEnd;

	public void runIO() throws IOException {
		timeBegin = System.currentTimeMillis();

		InputStream inputStream;
		OutputStream outputStream;

		if (ONLINE_JUDGE) {
			inputStream = System.in;
			Reader.init(inputStream);
			outputStream = System.out;

			out = new PrintWriter(outputStream);
		} else {
			inputStream = new FileInputStream("/home/peacefrog/Dropbox/IdeaProjects/Problem Solving/input");
			Reader.init(inputStream);
			out = new PrintWriter(System.out);
		}
		solve();
		out.flush();
		out.close();
		timeEnd = System.currentTimeMillis();
		System.err.println("Time = " + (timeEnd - timeBegin));
	}

	/*
	 * Start Solution Here
	 */

	private void solve() throws IOException {
		String n = Reader.next();

		long sum = 0;

		int curr ;

		for (int i = 0; i < n.length(); i++) {
			curr = n.charAt(i)-'0';
			if(curr % 4==0 ) {
				sum++;
			}

			if(i > 0){
				if(((n.charAt(i-1)-'0') *10 + curr)%4==0) sum+= i;
			}
		}

		out.println(sum);
	}

	public static void main(String[] args) throws IOException {
		new Task_B().runIO();
	}

	static class Reader {
		static BufferedReader reader;
		static StringTokenizer tokenizer;

		/**
		 * call this method to initialize reader for InputStream
		 */
		static void init(InputStream input) {
			reader = new BufferedReader(new InputStreamReader(input));
			tokenizer = new StringTokenizer("");
		}

		/**
		 * get next word
		 */
		static String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}

		static String nextLine() {
			try {
				return reader.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return "";
		}

		static int nextChar() throws IOException {
			return reader.read();
		}

		static int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		static long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		static double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		static long[] nextLongArray(int n) throws IOException {
			long[] arr = new long[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLong();
			}

			return arr;
		}

		static int[] nextIntArray(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextInt();
			}
			return arr;
		}
	}
}


import java.util.*;
import java.math.*;
import java.io.*;

public class Main
{

    public static void main(String[] args) throws Exception
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String s = in.readLine();
        long tot = 0;
        for (int i = 0; i < s.length(); i++)
        {
            if(Long.parseLong(String.valueOf(s.charAt(i)))% 4 == 0)
                tot++;
        }
        for (int i = 0; i <= s.length()-2; i++)
        {
            for (int j = i+1; j <= s.length()-1; j++)
            {
                if(Long.parseLong(s.substring(i,j+1))%4 ==0)
                    tot++;

            }

        }
        System.out.println(tot);
    }
}


import java.util.*;
import java.math.*;
import java.io.*;

public class Main
{

    public static void main(String[] args) throws Exception
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String s = in.readLine();
        long a = Long.parseLong(s);
        if(a<0)
            a = a * -1;
        s = a+ "";
        long tot = 0;
        for (int i = 0; i < s.length(); i++)
        {
            if(Long.parseLong(String.valueOf(s.charAt(i)))% 4 == 0)
                tot++;
        }
        for (int i = 0; i <= s.length()-2; i++)
        {
            for (int j = i+1; j <= s.length()-1; j++)
            {
                if(Long.parseLong(s.substring(i,j+1))%4 ==0)
                    tot++;

            }

        }
        System.out.println(tot);
    }
}


import java.util.*;
import java.math.*;
import java.io.*;

public class Main
{

    public static void main(String[] args) throws Exception
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String s = in.readLine();
        long tot = 0;
        for (int i = 0; i < s.length(); i++)
        {
            if(new BigInteger(String.valueOf(s.charAt(i))).mod(new BigInteger("4")).equals(new BigInteger("0")))
                tot++;
        }
        for (int i = 0; i <= s.length()-2; i++)
        {
            for (int j = i+1; j <= s.length()-1; j++)
            {
                if(new BigInteger(s.substring(i,j+1)).mod(new BigInteger("4")).equals(new BigInteger("0")))
                    tot++;

            }

        }
        System.out.println(tot);
    }
}


import java.util.*;
import java.math.*;
import java.io.*;

public class Main
{

    public static void main(String[] args) throws Exception
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String s = in.readLine();
        long tot = 0;
        for (int i = 0; i < s.length(); i++)
        {
            if((s.charAt(i)-'0')%4==0)
                tot++;
        }
        for (int i = s.length();i>=2;i--)
        {
                if(Integer.parseInt(s.substring(i-2,i))%4==0)
                    tot += i-1;
        }
        System.out.println(tot);
    }
}


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
import java.math.BigInteger;
/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Erasyl Abenov
 *
 *
 */
public class Main {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
            try (PrintWriter out = new PrintWriter(outputStream)) {
                TaskB solver = new TaskB();
                solver.solve(1, in, out);
            }
    }
}
class TaskB {
    public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException{
        String s = in.next();
        long ans = 0;
        for(int i = s.length() - 2; i >= 0; --i){
            int a = Integer.parseInt(s.charAt(i) +""+ s.charAt(i + 1));
            if(a%4 == 0) ans += i + 1;
        }
        for(int i = 0; i < s.length(); ++i)
            if((s.charAt(i) - '0')%4 == 0) ans++;
        out.println(ans);
    }
}
class InputReader {
    private final BufferedReader reader;
    private StringTokenizer tokenizer;
    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }

    public String nextLine() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(nextLine());
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }
    public BigInteger nextBigInteger(){
        return new BigInteger(next());
    }
    public long nextLong() {
        return Long.parseLong(next());
    }
    public double nextDouble() {
        return Double.parseDouble(next());
    }
}


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
import java.math.BigInteger;
/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Erasyl Abenov
 *
 *
 */
public class Main {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
            try (PrintWriter out = new PrintWriter(outputStream)) {
                TaskB solver = new TaskB();
                solver.solve(1, in, out);
            }
    }
}
class TaskB {
    public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException{
        String s = in.next();
        int ans = 0;
        for(int i = s.length() - 2; i >= 0; --i){
            int a = Integer.parseInt(s.charAt(i) +""+ s.charAt(i + 1));
            if(a%4 == 0) ans += i + 1;
        }
        for(int i = 0; i < s.length(); ++i)
            if((s.charAt(i) - '0')%4 == 0) ans++;
        out.println(ans);
    }
}
class InputReader {
    private final BufferedReader reader;
    private StringTokenizer tokenizer;
    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }

    public String nextLine() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(nextLine());
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }
    public BigInteger nextBigInteger(){
        return new BigInteger(next());
    }
    public long nextLong() {
        return Long.parseLong(next());
    }
    public double nextDouble() {
        return Double.parseDouble(next());
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Codef{
    public static void main(String[] args) throws IOException{
        BufferedReader vod=new BufferedReader(new InputStreamReader(System.in));
        String s=new String(vod.readLine());
        int ans=0;
        for (int i=s.length()-1; i>=1; i--){
            int x=Integer.parseInt(s.substring(i-1,i+1));
            if (x%4==0)
                ans+=i;
            x=Integer.parseInt(s.substring(i,i+1));
            if (x%4==0) ans++;
        }
        if (Integer.parseInt(s.substring(0,1))%4==0)
            ans++;
        System.out.print(ans);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Codef{
    public static void main(String[] args) throws IOException{
        BufferedReader vod=new BufferedReader(new InputStreamReader(System.in));
        String s=new String(vod.readLine());
        long ans=0;
        for (int i=s.length()-1; i>=1; i--){
            int x=Integer.parseInt(s.substring(i-1,i+1));
            if (x%4==0)
                ans+=i;
            x=Integer.parseInt(s.substring(i,i+1));
            if (x%4==0) ans++;
        }
        if (Integer.parseInt(s.substring(0,1))%4==0)
            ans++;
        System.out.print(ans);
    }
}


import java.util.*;

public class NewSkateboard {
    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        String s=in.next();
        long cont=0; int cs,cc;
        for (int i = 0; i < s.length(); i++) {
            cs=s.charAt(i)-'0';
            if(cs%4==0)
                cont++;
        }
        for (int i = 0,j=1;j<s.length(); i++,j++) {
//            cs=s.charAt(i)-'0';
//            if(cs%4==0)
//                cont++;
            cc=(s.charAt(i)-'0')*10 +(s.charAt(j)-'0');
            if (cc%4==0)
                cont= j+cont;
        }
        System.out.println(cont);
    }
}


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        String s=in.next();
        int cont=0,cs,cc;
        for (int i = 0; i < s.length(); i++) {
            cs=s.charAt(i)-'0';
            if(cs%4==0)
                cont++;
        }
        for (int i = 0,j=1;j<s.length(); i++,j++) {
//            cs=s.charAt(i)-'0';
//            if(cs%4==0)
//                cont++;
            cc=(s.charAt(i)-'0')*10 +(s.charAt(j)-'0');
            if (cc%4==0)
                cont= j+cont;
        }
        System.out.println(cont);

    }
}


import java.util.*;

public class B {

	public static void main(String[] args) {

		Scanner qwe = new Scanner(System.in);

		char[] in = qwe.next().toCharArray();
		int[] digs = new int[in.length];

		long ans = 0;
		for (int i = 0; i < digs.length; i++) {
			digs[i] = in[i]-'0';
			if(digs[i] % 4 == 0) ans++;
			if(i > 0 && (10*digs[i-1] +digs[i]) % 4 == 0){
				ans += i;
			}
		}

		System.out.println(ans);

		qwe.close();
	}

}


import java.io.*;
public class B {
    public static void main (String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String num = reader.readLine();
        int[] arr = new int[num.length()];
        for (int i = 0; i < num.length(); i++) {
            arr[i] = Integer.parseInt(Character.toString(num.charAt(i)));
        }
        long res = 0;

        for (int i = num.length() - 1; i >= 0; i--) {
            if (arr[i] % 2 == 0) {
                if ((arr[i] == 0) || (arr[i] == 4) || (arr[i] == 8)) {
                    res++;
                    if ((i != 0) && (arr[i - 1] % 2 == 0)) {
                        res += i;
                    }
                }
                else {
                    if ((i != 0) && (arr[i - 1] % 2 == 1)) {
                        res += i;
                    }
                }
            }
        }

        System.out.println(res);
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{

	// split de array
	public static int[] readInts(String cad) {
		String read[] = cad.split(" ");
		int res[] = new int[read.length];
		for (int i = 0; i < read.length; i++) {
			res[i] = Integer.parseInt(read[i]);
		}
		return res;
	}

	public static int print(int k) {
		int cont = 0;
		while (k > 0) {
			cont += k % 10;
			k /= 10;
		}
		return cont;
	}

	public static void main(String[] args) throws IOException {
		long inicio = System.currentTimeMillis();
		StringBuilder out = new StringBuilder();
		BufferedReader in;
		File archivo = new File("entrada");
		if (archivo.exists())
			in = new BufferedReader(new FileReader(archivo));
		else
			in = new BufferedReader(new InputStreamReader(System.in));

		int arr[] = readCad(in.readLine());
		long count = 0;
		for (int i = arr.length - 1; i > 0; i--) {
			int r = Integer.parseInt(arr[i - 1] + "" + arr[i]);
			if (r % 4 == 0) {
				count += i;
			}
			if (arr[i] % 4 == 0)
				count++;
		}
		if( arr[0] %4 == 0)
			count++;
		System.out.println(count);
	}

	public static int[] readCad(String cad) {
		int arr[] = new int[cad.length()];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = cad.charAt(i) - '0';
		}
		return arr;
	}
}


import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        long a ;
        long tot= 0L;
        for (int i = 0; i < s.length(); i++) {
            int n = s.charAt(i)-'0';
            if( n % 4 == 0)
                tot++;

        }
        for (int i = 0,j = 1 ; i < s.length()-1 && j < s.length(); i++ , j++) {
            int x = s.charAt(i)- '0';
            int y = s.charAt(j)- '0';
            if( ((x*10) + y) % 4 ==0 )
                tot+= i+1;

        }
        System.out.println(tot);

    }
}


import java.util.*;
import java.io.*;
public class Maths {
public static void main(String [] args)throws Exception{
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	String str = br.readLine();
	long ans = 0;
	int array[][] = new int[str.length() + 1][4];
	for(int i = 0;i < str.length();i++){
		int mod = (str.charAt(i) - 48) % 4;
		array[i + 1][mod]++;
		for(int j = 0;j < 4;j++){
			//System.out.println(mod + " " + (i + 1) + " " + j);
			array[i + 1][(j*10 + mod) % 4] += array[i][j];
		}
		ans += array[i + 1][0];
	}
	System.out.print(ans);
}
}


import java.io.*;
import java.util.*;
public class B628 {
public static void main(String[] args) throws IOException {
	input.init(System.in);
	PrintWriter out = new PrintWriter(System.out);
	char[] s = input.next().toCharArray();
	int n = s.length;
	long res = 0;
	for(int i = 0; i<n; i++)
	{
		int x = s[i] - '0';
		if(x%4 == 0) res++;
		if(i < n - 1 && (x*10 + (s[i+1] - '0'))%4 == 0) res += i+1;
	}
	out.println(res);
	out.close();
}

public static class input {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens())
			tokenizer = new StringTokenizer(reader.readLine());
		return tokenizer.nextToken();
	}

	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}

	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}
}
}


import java.util.*;

public class test {
	public static void main (String[] args){

		Scanner enter = new Scanner(System.in);

		String s=enter.nextLine();

		int res=0;

		for (int i=0;i<s.length();i++){

			if(i<=s.length()-2){
				String su = s.substring(i, i+2);
                int sunum = Integer.parseInt(su);

                if (sunum%4==0){
                	res+=1+i;
                }

			}
			if (s.charAt(i)%4==0){
				res+=1;
			}

		}

		System.out.println(res);

		enter.close();

	}

}


import java.util.*;

public class test {
	public static void main (String[] args){

		Scanner enter = new Scanner(System.in);

		String s=enter.nextLine();

		long res=0;

		for (int i=0;i<s.length();i++){

			if(i<=s.length()-2){
				String su = s.substring(i, i+2);
                int sunum = Integer.parseInt(su);

                if (sunum%4==0){
                	res+=1+i;
                }

			}
			if (s.charAt(i)%4==0){
				res+=1;
			}

		}

		System.out.println(res);

		enter.close();

	}

}


import java.util.*;

public class test {
	public static void main (String[] args){

		Scanner enter = new Scanner(System.in);

		String s=enter.nextLine();

		int res=0;

		for (int i=0;i<s.length();i++){

			if(i<s.length()-2){
				String su = s.substring(i, i+2);
                int sunum = Integer.parseInt(su);
                if (sunum%4==0){
                	res+=1+i;
                }

			}
			if (s.charAt(i)%4==0){
				res+=1;
			}

		}

		System.out.println(res);

		enter.close();

	}

}


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main{
    public static void main(String[] args) throws Exception{
        Scanny in = new Scanny();
        String s = in.next();
        long ans = 0;
        int x, y;
        for (int i = 0, j = 1; j < s.length(); i++, j++) {
            x = s.charAt(i)-'0';
            y = s.charAt(j)-'0';
            if(x%4==0)
                ans++;
            if((x*10+y)%4==0)
                ans+=i+1;
            if(j==s.length()-1 && y%4==0)
                ans++;
        }
        System.out.println(ans);
    }
}

class Scanny {
   BufferedReader br;
   StringTokenizer st;
   Scanny() {
      br = new BufferedReader(new InputStreamReader(System.in));
   }
   Scanny(String fn) throws IOException {
      br = new BufferedReader(new FileReader(fn));
   }
   boolean hasNext() throws IOException {
      String s = br.readLine();
      if (s == null) return false;
      st = new StringTokenizer(s);
      return true;
   }
   String next() throws IOException {
      while (st == null || !st.hasMoreTokens())
         st = new StringTokenizer(br.readLine());
      return st.nextToken();
   }
   int nextInt() throws IOException { return Integer.parseInt(next()); }
   long nextLong() throws IOException { return Long.parseLong(next()); }
   double nextDouble() throws IOException { return Double.parseDouble(next()); }
   String nextLine() throws IOException { return br.readLine(); }
}


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception{
        Scanny in = new Scanny();
        String s = in.next();
        long ans = 0;
        int x, y;
        if(s.length()==1){
            if((s.charAt(0)-'0')%4==0)
                ans++;
        }
        else{
            for (int i = 0, j = 1; j < s.length(); i++, j++) {
                x = s.charAt(i)-'0';
                y = s.charAt(j)-'0';
                if(x%4==0)
                    ans++;
                if((x*10+y)%4==0)
                    ans+=i+1;
                if(j==s.length()-1 && y%4==0)
                    ans++;
            }
        }
        System.out.println(ans);
    }
}

class Scanny {
   BufferedReader br;
   StringTokenizer st;
   Scanny() {
      br = new BufferedReader(new InputStreamReader(System.in));
   }
   Scanny(String fn) throws IOException {
      br = new BufferedReader(new FileReader(fn));
   }
   boolean hasNext() throws IOException {
      String s = br.readLine();
      if (s == null) return false;
      st = new StringTokenizer(s);
      return true;
   }
   String next() throws IOException {
      while (st == null || !st.hasMoreTokens())
         st = new StringTokenizer(br.readLine());
      return st.nextToken();
   }
   int nextInt() throws IOException { return Integer.parseInt(next()); }
   long nextLong() throws IOException { return Long.parseLong(next()); }
   double nextDouble() throws IOException { return Double.parseDouble(next()); }
   String nextLine() throws IOException { return br.readLine(); }
}


import java.util.*;
import java.io.*;

public class B {
    public static void main(String[] args) throws IOException {
	BufferedReader in =
	    new BufferedReader(new InputStreamReader(System.in));
	PrintWriter out =
	    new PrintWriter(new OutputStreamWriter(System.out));
	char[] nums = in.readLine().toCharArray();
	long numways = 0;
	for (char c : nums) {
	    if (c == '0' || c == '4' || c == '8') numways++;
	}
	for (int i = 0; i < nums.length-1; i++) {
	    if (div4(nums[i], nums[i+1])) numways += (long)(i+1);
	}
	out.println(numways);
	out.close();
    }

    static boolean div4(char c1, char c2) {
	int num = Character.getNumericValue(c1)*10+Character.getNumericValue(c2);
	return (num%4)==0;
    }
}


import java.util.*;
import java.io.*;
public class Main {
	public static void main(String[] args) {
		Scanner stdin = new Scanner(new BufferedInputStream(System.in));
		while (stdin.hasNext()) {
			String s = stdin.nextLine();
			long ans = 0;
			for (int i = 0; i < s.length(); i++) {
				int num = s.charAt(i) - '0';
				if (num % 4 == 0)
					ans++;
			}
			for (int i = 1; i < s.length(); i++) {
				int num = (s.charAt(i - 1) - '0') * 10;
				num += s.charAt(i) - '0';
				if (num % 4 == 0)
					ans += i;
			}
			System.out.println(ans);
		}
	}
}


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class P628B {
  InputStream is;
  PrintWriter out;
  String INPUT = "04";

  void solve() {
    String str = ns();
    long res = 0;
    for (int i = str.length(); i > 0; i--) {
      if (Integer.parseInt(str.substring(i - 1, i)) % 4 == 0) {
        res++;
      }
      if (i > 1) {
        int num = Integer.parseInt(str.substring(i - 2, i));
        if (num % 4 == 0) {
          res += (i - 2) + 1;
        }
      }
    }
    System.out.println(res);
  }

  public static void main(String[] args) throws Exception {
    new P628B().run();
  }

  void run() throws Exception {
    is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
    out = new PrintWriter(System.out);

    long s = System.currentTimeMillis();
    solve();
    out.flush();
    tr(System.currentTimeMillis() - s + "ms");
  }

  private byte[] inbuf = new byte[1024];
  private int lenbuf = 0, ptrbuf = 0;

  private int readByte() {
    if (lenbuf == -1)
      throw new InputMismatchException();
    if (ptrbuf >= lenbuf) {
      ptrbuf = 0;
      try {
        lenbuf = is.read(inbuf);
      } catch (IOException e) {
        throw new InputMismatchException();
      }
      if (lenbuf <= 0)
        return -1;
    }
    return inbuf[ptrbuf++];
  }

  private boolean isSpaceChar(int c) {
    return !(c >= 33 && c <= 126);
  }

  private int skip() {
    int b;
    while ((b = readByte()) != -1 && isSpaceChar(b))
      ;
    return b;
  }

  private double nd() {
    return Double.parseDouble(ns());
  }

  private char nc() {
    return (char) skip();
  }

  private String ns() {
    int b = skip();
    StringBuilder sb = new StringBuilder();
    while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
      sb.appendCodePoint(b);
      b = readByte();
    }
    return sb.toString();
  }

  private char[] ns(int n) {
    char[] buf = new char[n];
    int b = skip(), p = 0;
    while (p < n && !(isSpaceChar(b))) {
      buf[p++] = (char) b;
      b = readByte();
    }
    return n == p ? buf : Arrays.copyOf(buf, p);
  }

  private char[][] nm(int n, int m) {
    char[][] map = new char[n][];
    for (int i = 0; i < n; i++)
      map[i] = ns(m);
    return map;
  }

  private int[] na(int n) {
    int[] a = new int[n];
    for (int i = 0; i < n; i++)
      a[i] = ni();
    return a;
  }

  private int ni() {
    int num = 0, b;
    boolean minus = false;
    while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
      ;
    if (b == '-') {
      minus = true;
      b = readByte();
    }

    while (true) {
      if (b >= '0' && b <= '9') {
        num = num * 10 + (b - '0');
      } else {
        return minus ? -num : num;
      }
      b = readByte();
    }
  }

  private long nl() {
    long num = 0;
    int b;
    boolean minus = false;
    while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
      ;
    if (b == '-') {
      minus = true;
      b = readByte();
    }

    while (true) {
      if (b >= '0' && b <= '9') {
        num = num * 10 + (b - '0');
      } else {
        return minus ? -num : num;
      }
      b = readByte();
    }
  }

  private boolean oj = System.getProperty("ONLINE_JUDGE") != null;

  private void tr(Object... o) {
    if (!oj)
      System.out.println(Arrays.deepToString(o));
  }
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 * Feb 19, 2016 | 4:52:52 PM
 *

 * Description
 *
 *

 *
 * @author Essiennta Emmanuel (colourfulemmanuel@gmail.com)
 */
public class ProblemB{

    long solve(char[] s){
        long sum = (s[0] - '0') % 4 == 0 ? 1 : 0;
        for (int i = 1; i < s.length; i++){
            int num = (s[i - 1] - '0') * 10 + s[i] - '0';
            if (num % 4 == 0)
                sum += i;
            if ((s[i] - '0') % 4 == 0)
                sum++;
        }
        return sum;
    }

    public static void main(String[] args) throws IOException{
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        out.println(new ProblemB().solve(in.readLine().toCharArray()));
        out.close();
        in.close();
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class B {
	static boolean ok(int a, int b) {
		return ((a * 10 + b) % 4 == 0);
	}

	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		String line = Reader.nextLine();
		int n = line.length();
		long rez = 0;
		for (int i = 0; i < n - 1; i++) {
			if ((line.charAt(i) - '0') % 4 == 0)
				rez++;
			if (ok(line.charAt(i) - '0', line.charAt(i + 1) - '0')) {
				rez++;
				rez+=i;
			}
		}
		if((line.charAt(n-1)-'0')%4==0)
			rez++;
		System.out.println(rez);
	}
}

class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	/** call this method to initialize reader for InputStream */
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	/** get next word */
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static String nextLine() throws IOException {
		return reader.readLine();
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
}


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Scanner;

public class Palindrome
{
	public static void main(String[] args) throws IOException {

		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String s=br.readLine();
		long total = 0;

		//int[][] dp=new int[s.length()][s.length()];
		for(int i=0;i<s.length();i++)
		{
			//modulo functions priority is more than other
			if((s.charAt(i)-'0')%4==0)
				{
				//dp[i][i]=1;
				total++;
				}
		}
		for(int i=s.length();i>=2;i--)
		{
			//System.out.println(s.substring(i-2, i));
			if((Integer.parseInt(s.substring(i-2, i))%4)==0)
			{
				for(int j=0;j<i-1;j++)
					{
					//dp[j][i-1]=1;
					total++;
					}
			}
		}
		PrintWriter pw=new PrintWriter(System.out);
		System.out.println(total);
	}
}


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Scanner;

public class Palindrome
{
	public static void main(String[] args) throws IOException {

		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String s=br.readLine();
		long total = 0;

		//int[][] dp=new int[s.length()][s.length()];
		for(int i=0;i<s.length();i++)
		{
			//modulo functions priority is more than other
			if((s.charAt(i)-'0')%4==0)
				{
			//	dp[i][i]=1;
				total++;
				}
		}
		for(int i=s.length();i>=2;i--)
		{
			//System.out.println(s.substring(i-2, i));
			if((Integer.parseInt(s.substring(i-2, i))%4)==0)
			{
				total=total+i-1;
			}
		}

		System.out.println(total);
	}
}


import java.util.Scanner;

public class Palindrome
{
	public static void main(String[] args) {

		Scanner sc=new Scanner(System.in);
		String s=sc.nextLine();
		int total = 0;
		int[][] dp=new int[s.length()][s.length()];
		for(int i=0;i<s.length();i++)
		{
			//modulo functions priority is more than other
			if((s.charAt(i)-'0')%4==0)
				{
				dp[i][i]=1;
				total++;
				}
		}
		for(int i=s.length();i>=2;i--)
		{
			//System.out.println(s.substring(i-2, i));
			if((Integer.parseInt(s.substring(i-2, i))%4)==0)
			{
				for(int j=0;j<i-1;j++)
					{
					dp[j][i-1]=1;
					total++;
					}
			}
		}
		System.out.println(total);
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{
        int n;
        scanf("%d",&n);
        string a;
        cin>>a;
        if(n%2!=0)
        cout<<-1;
        else
        {
                int cnt=0,pnt=0,mnt=0;
                for(int i=0;i<n-1;i+=2)
                {
                               (a[i]=='(')?cnt++:pnt++;

                               (a[i+1]=='(')?cnt++:pnt++;

                               if((a[i]=='('&&a[i+1]==')'))
                               {

                                                cnt--;
                                                pnt--;

                               }

                }
                if(cnt!=pnt)
                cout<<-1;
                else
                cout<<cnt+pnt;
        }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
        long long int n,blue=0;
        string a;
        scanf("%d",&n);
        cin>>a;
        long long int vnt=0,cnt=0;
        for(long long int i=0;i<n;i++)
        {
        {        if(a[i]=='(')
                vnt++;
                else
                cnt++;
        }
        if(a[i]=='('&&a[i+1]!=')'&&i=n-2)
        blue++;
        }
        if(vnt!=cnt)
        {
               cout<<-1;
        }
        else
        {
        cout<<2*blue;
        }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
        long long int n,blue=0;
        string a;
        scanf("%d",&n);
        cin>>a;
        long long int vnt=0,cnt=0;
        for(long long int i=0;i<n;i++)
        {
        {        if(a[i]==40)
                vnt++;
                else
                cnt++;
        }
        if(a[i]==40&&a[i+1]!=41&&i==n-2)
        blue++;
        }
        if(vnt!=cnt)
        {
               cout<<-1;
        }
        else
        {
        cout<<2*blue;
        }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
        long long int n,blue=0;
        string a;
        scanf("%d",&n);
        cin>>a;
        long long int vnt=0,cnt=0;
        for(long long int i=0;i<n;i++)
        {
        {        if(a[i]=='(')
                vnt++;
                else
                cnt++;
        }
        if(a[i]=='('&&a[i+1]!=')'&&i==n-2)
        blue++;
        }
        if(vnt!=cnt)
        {
               cout<<-1;
        }
        else
        {
        cout<<2*blue;
        }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
	#endif
	int n;
	cin>>n;
	if(n%2!=0)
	{
		cout<<-1;
		return 0;
	}
	string a;
	cin>>a;
	int x1=0,x2=0,mnt=0,pnt=0;
	for(int i=0;i<n;i++)
	{
		if(a[i]==')')
		{
		        x1++;
			mnt--;
			if(mnt<0)
			pnt++;
		}
		else
		{
		        x2++;
			if(mnt<0)
			{
				pnt++;
			}
			mnt++;
		}
	}
	if(pnt%2==0&&x1==x2)
		cout<<pnt;
	else
		cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
        long long int n,blue=0;
        string a;
        scanf("%d",&n);
        cin>>a;
        long long int vnt=0,cnt=0;
        for(long long int i=0;i<n;i++)
        {
        {        if(a[i]=="(")
                vnt++;
                else
                cnt++;
        }
        if(a[i]=="("&&a[i+1]!=")"&&i==n-2)
        blue++;
        }
        if(vnt!=cnt)
        {
               cout<<-1;
        }
        else
        {
        cout<<2*blue;
        }
}


#include<iostream>
#include<cstdio>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include <map>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <limits.h>
#include <string>
#include<functional>
#include <set>
#include <utility>
#include <iomanip>
#include <string>
#define INF 1<<30
#define MO 1000000007
#define MI 998244353
#define ll long long
using namespace std;
char str[1000010];
int main()
{
   int n;scanf("%d",&n);
   getchar();gets(str);
   if(n%2) printf("-1\n");
   else
   {
       int ans=0;
       for(int i=n-1;i>=0;--i)
       {
          if(str[i]=='(')
          {
              int a=1,len=0;
              while(a&&i>=0)
              {
                 --i;
                 if(str[i]==')') --a;
                 else if(str[i]=='(') ++a;
                 ++len;
              }
              ans+=len+1;
          }
          else if(str[i]==')')
          {
              int a=1;
              while(a&&i>=0)
              {
                 --i;
                 if(str[i]=='(') a--;
                 else if(str[i]==')') a++;
              }
          }
       }
       printf("%d\n",ans);
   }
}


#include<iostream>
#include<cstdio>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include <map>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <limits.h>
#include <string>
#include<functional>
#include <set>
#include <utility>
#include <iomanip>
#include <string>
#define INF 1<<30
#define MO 1000000007
#define MI 998244353
#define ll long long
using namespace std;
char str[1000010];
int panduan(int s)
{
    int a=1;
    for(int i=s-1;i>=0;i--)
    {
     if(str[i]==')') a--;
     else if(str[i]=='(') a++;
     if(!a) return i;
    }

}
int panduan2(int s)
{
    int a=1;
    for(int i=s-1;i>=0;i--)
    {
     if(str[i]=='(') a--;
     else if(str[i]==')') a++;
     if(!a) return i;
    }
}
int main()
{
   int n;scanf("%d",&n);
   getchar();gets(str);
   if(n%2) printf("-1\n");
   else
   {
       int a=0,b=0,ans=0;
       for(int i=n-1;i>=0;i--)
       {
          if(str[i]=='(')
          {
              int len=panduan(i);
              ans+=i-len+1;i=len;
          }
          else if(str[i]==')')
          {
              int len=panduan2(i);
              i=len;
          }
       }
       printf("%d\n",ans);
   }
}


#include<iostream>
#include<cstdio>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include <map>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <limits.h>
#include <string>
#include<functional>
#include <set>
#include <utility>
#include <iomanip>
#include <string>
#define INF 1<<30
#define MO 1000000007
#define MI 998244353
#define ll long long
using namespace std;
char str[1000010];
int main()
{
   int n;scanf("%d",&n);
   getchar();gets(str);
   if(n%2) printf("-1\n");
   else
   {
       int ans=0,ai=0,bi=0;
       for(int i=n-1;i>=0;--i)
       {
          if(str[i]=='(')
          {
              ai++;
              int a=1,len=0;
              while(a&&i>=0)
              {
                 --i;
                 if(str[i]==')') {--a;bi++;}
                 else if(str[i]=='(') {++a;ai++}
                 ++len;
              }
              ans+=len+1;
          }
          else if(str[i]==')')
          {
              int a=1;bi++;
              while(a&&i>=0)
              {
                 --i;
                 if(str[i]=='(') {a--;ai++;}
                 else if(str[i]==')') {a++;bi++}
              }
          }
       }
       if(ai==bi)
       printf("%d\n",ans);
       else printf("-1\n");
   }
}


#include<iostream>
#include<cstdio>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include <map>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <limits.h>
#include <string>
#include<functional>
#include <set>
#include <utility>
#include <iomanip>
#include <string>
#define INF 1<<30
#define MO 1000000007
#define MI 998244353
#define ll long long
using namespace std;
char str[1000010];
int main()
{
   int n;scanf("%d",&n);
   getchar();gets(str);
   if(n%2) printf("-1\n");
   else
   {
       int ans=0,ai=0,bi=0;
       for(int i=n-1;i>=0;--i)
       {
          if(str[i]=='(')
          {
              ai++;
              int a=1,len=0;
              while(a&&i>=0)
              {
                 --i;
                 if(str[i]==')') {--a;bi++;}
                 else if(str[i]=='(') {++a;ai++;}
                 ++len;
              }
              ans+=len+1;
          }
          else if(str[i]==')')
          {
              int a=1;bi++;
              while(a&&i>=0)
              {
                 --i;
                 if(str[i]=='(') {a--;ai++;}
                 else if(str[i]==')') {a++;bi++;}
              }
          }
       }
       if(ai==bi)
       printf("%d\n",ans);
       else printf("-1\n");
   }
}


#include<iostream>
#include<cstdio>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include <map>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <limits.h>
#include <string>
#include<functional>
#include <set>
#include <utility>
#include <iomanip>
#include <string>
#define INF 1<<30
#define MO 1000000007
#define MI 998244353
#define ll long long
using namespace std;
char str[1000010];
int main()
{
   int n;scanf("%d",&n);
   getchar();gets(str);
   if(n%2) printf("-1\n");
   else
   {
       int ans=0;
       for(int i=n-1;i>=0;--i)
       {
          if(str[i]=='(')
          {
              int a=1,len=0;
              while(a)
              {
                 --i;
                 if(str[i]==')') --a;
                 else if(str[i]=='(') ++a;
                 ++len;
              }
              ans+=len+1;
          }
          else if(str[i]==')')
          {
              int a=1;
              while(a)
              {
                 --i;
                 if(str[i]=='(') a--;
                 else if(str[i]==')') a++;
              }
          }
       }
       printf("%d\n",ans);
   }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	// your code goes here
	ll n,a=0,b=0;
	//list<int> stac;
	cin>>n;
	string s;
	cin>>s;
	for(int i=0;i<s.size();i++)
	{
		int f = a<0;
		if(s[i]=='(')
			a++;
		else
			a--;
		f|=(a<0);
		if(f)
			b++;
	}
	if(a!=0)
		cout<<-1<<endl;
	else
		cout<<b<<endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long

int main() {
	// your code goes here
	ll n,a=0,b=0;
	//list<int> stac;
	cin>>n;
	string s;
	cin>>s;
	for(int i=0;i<s.size();i++)
	{
		int f = a<0;
		if(s[i]=='(')
			a++;
		else
			a--;
		f|=(a<0);
		if(f)
			b++;
	}
	if(a!=0)
		cout<<-1<<endl;
	else
		cout<<b<<endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long

int main() {
	// your code goes here
	ll n,a=0,b=0,sum=0,f=1;
	cin>>n;
	string s;
	cin>>s;
	if(n&1)
	{
		cout<<-1<<endl;
		f=0;
	}
	for(int i=0;i<s.size() && f;i++)
	{
		if(s[i]=='(')
		{
			a++;
			if(a==b)
			{
				sum+=2*a;
				a=0;
				b=0;
			}
		}
		else
		{
			if(a)
				a--;
			else
				b++;
		}
	}
	if(f)
		cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	long long int n,l,r,i,j,sum=0,f=0;

	cin>>n;
	string s;
	cin>>s;

	if(n%2!=0)
	{
		cout<<"-1"<<endl;
		return 0;
	}

    for(i=0;i<s.size();i+=2)
    {
    	if(s[i]=='(' && s[i+1]==')')
    	{
    		continue;
    	}
    	else
    	sum+=2;

    }

	if(sum%2!=0)
	{
		cout<<"-1";
	}
	else
	cout<<sum;

}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	long long int n,l,r,i,j,sum=0,f=0;

	cin>>n;
	string s,p="";
	cin>>s;

	if(n%2!=0)
	{
		cout<<"-1"<<endl;
		return 0;
	}

    long long int arr[n+1]={0};
    for(i=0;i<s.size();i++)
    {
    	if(s[i]=='(')
    	{
    		arr[i+1]=arr[i]+1;
    	}
    	else
    	{
    		arr[i+1]=arr[i]-1;
    	}
    }

    k=0;
    for(i=1;i<n+1;i++)
    {
    	if(arr[i]<0 && arr[i-1]<=0)
    	{
    		sum++;
    	}
    	else if(arr[i]<=0 && arr[i-1]<0)
    	{
    		sum++;
    	}
    }
    i=n;
    if(arr[i]>0)
    {
    	j=0;
    	while(s[i]!=0 && i>0)
    	{
    		i--;
    		j++;
    	}
    	sum=sum+(j/2);
    }
    if(sum%2==0)
	cout<<sum;
	else
	cout<<"-1";

}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	long long int n,l,r,i,j,sum=0,f=0;

	cin>>n;
	string s,p="";
	cin>>s;

	if(n%2!=0)
	{
		cout<<"-1"<<endl;
		return 0;
	}

    long long int arr[n+1]={0};
    for(i=0;i<s.size();i++)
    {
    	if(s[i]=='(')
    	{
    		arr[i+1]=arr[i]+1;
    	}
    	else
    	{
    		arr[i+1]=arr[i]-1;
    	}
    }

    for(i=1;i<n+1;i++)
    {
    	if(arr[i]<0 && arr[i-1]<=0)
    	{
    		sum++;
    	}
    	else if(arr[i]<=0 && arr[i-1]<0)
    	{
    		sum++;
    	}
    }

	cout<<sum;

}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	long long int n,l,r,i,j,sum=0,f=0;

	cin>>n;
	string s,p="";
	cin>>s;

	if(n%2!=0)
	{
		cout<<"-1"<<endl;
		return 0;
	}

    long long int arr[n+1]={0};
    for(i=0;i<s.size();i++)
    {
    	if(s[i]=='(')
    	{
    		arr[i+1]=arr[i]+1;
    	}
    	else
    	{
    		arr[i+1]=arr[i]-1;
    	}
    }

    for(i=1;i<n+1;i++)
    {
    	if(arr[i]<0 && arr[i-1]<=0)
    	{
    		sum++;
    	}
    	else if(arr[i]<=0 && arr[i-1]<0)
    	{
    		sum++;
    	}
    }
    if(!arr[n])
	cout<<sum;
	else
	cout<<"-1";

}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	long long int n,l,r,i,j,sum=0,f=0;

	cin>>n;
	string s,p="";
	cin>>s;

	if(n%2!=0)
	{
		cout<<"-1"<<endl;
		return 0;
	}

    long long int arr[n+1]={0};
    for(i=0;i<s.size();i++)
    {
    	if(s[i]=='(')
    	{
    		arr[i+1]=arr[i]+1;
    	}
    	else
    	{
    		arr[i+1]=arr[i]-1;
    	}
    }

    for(i=1;i<n+1;i++)
    {
    	if(arr[i]<0 && arr[i-1]<=0)
    	{
    		sum++;
    	}
    	else if(arr[i]<=0 && arr[i-1]<0)
    	{
    		sum++;
    	}
    }
    i=n;
    if(arr[i]>0)
    {
    	j=0;
    	while(s[i]!=0 && i>0)
    	{
    		i--;
    		j++;
    	}
    	sum=sum+(j/2);
    }
    if(sum%2==0)
	cout<<sum;
	else
	cout<<"-1";

}


#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i=n-1 ; i > =0; i--)
#define all(a) a.begin(),a.end()
#define pb push_back
using namespace std;
typedef long long int ll;
struct pair_hash {
    inline std::size_t operator()(const std::pair<int,int> & v) const {
        return v.first*31+v.second;
    }
};
void solve(){

    int q;
    cin>>q;
    string s;
    cin>>s;
    if(q%2!=0)
        cout<<"-1";
    else
    {
        int balance=0,start=0,end=0;
        for(int i=0;i<q;i++)
        {
            if(s[i]=='(')
            {
                balance++;
                if(balance>0)start++;
            }
            else
            {
                balance--;
                if(balance<0)end++;
            }
        }
        cout<<2*max(start,end);

    }

}
int main(){

    //#ifndef ONLINE_JUDGE
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //#endif
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;

  solve();
}


#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i=n-1 ; i > =0; i--)
#define all(a) a.begin(),a.end()
#define pb push_back
using namespace std;
typedef long long int ll;
struct pair_hash {
    inline std::size_t operator()(const std::pair<int,int> & v) const {
        return v.first*31+v.second;
    }
};
void solve(){

    int q;
    cin>>q;
    string s;
    cin>>s;
    if(q%2!=0)
        cout<<"-1";
    else
    {
        int balance=0,start=0,end=0,count=0;
        for(int i=0;i<q;i++)
        {
            if(s[i]=='(')
            {
                balance++;
                if(balance==0)count+=i-start+1;

            }
            else
            {
                balance--;
                if(balance==-1)start=i;
                // cout<<start<<" ";
            }

        }
        cout<<count<<" "<<endl;

    }

}
int main(){

    //#ifndef ONLINE_JUDGE
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //#endif
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;

  solve();
}


#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i=n-1 ; i > =0; i--)
#define all(a) a.begin(),a.end()
#define pb push_back
using namespace std;
typedef long long int ll;
struct pair_hash {
    inline std::size_t operator()(const std::pair<int,int> & v) const {
        return v.first*31+v.second;
    }
};
void solve(){

    ll q;
    cin>>q;
    string s;
    cin>>s;
    if(q%2!=0)
        cout<<"-1";
    else
    {
        ll balance=0,start=0,end=0,count=0,c1=0,c2=0;
        bool ok=true;
        for(ll i=0;i<q;i++)
        {
            if(s[i]=='(')
            {
                c1++;
                balance++;
                if(balance==0)count+=i-start+1;

            }
            else
            {
                c2++;
                balance--;
                if(balance==-1)start=i,ok=false;
                // cout<<start<<" ";
            }

        }
        if(c1==c2)
        cout<<count;
    else
        cout<<"-1"<<start<<" "<<end;

    }

}
int main(){

    //#ifndef ONLINE_JUDGE
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //#endif
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;

  solve();
}


#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i=n-1 ; i > =0; i--)
#define all(a) a.begin(),a.end()
#define pb push_back
using namespace std;
typedef long long int ll;
struct pair_hash {
    inline std::size_t operator()(const std::pair<int,int> & v) const {
        return v.first*31+v.second;
    }
};
void solve(){

    ll q;
    cin>>q;
    string s;
    cin>>s;
    if(q%2!=0)
        cout<<"-1";
    else
    {
        ll balance=0,start=0,end=0,count=0;
        bool ok=true;
        for(ll i=0;i<q;i++)
        {
            if(s[i]=='(')
            {
                start++;
                balance++;
                if(balance==0)count+=i-start+1;

            }
            else
            {
                end++;
                balance--;
                if(balance==-1)start=i,ok=false;
                // cout<<start<<" ";
            }

        }
        if(start==end)
        cout<<count;
    else
        cout<<"-1";

    }

}
int main(){

    //#ifndef ONLINE_JUDGE
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //#endif
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;

  solve();
}


#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i=n-1 ; i > =0; i--)
#define all(a) a.begin(),a.end()
#define pb push_back
using namespace std;
typedef long long int ll;
struct pair_hash {
    inline std::size_t operator()(const std::pair<int,int> & v) const {
        return v.first*31+v.second;
    }
};
void solve(){

    ll q;
    cin>>q;
    string s;
    cin>>s;
    if(q%2!=0)
        cout<<"-1";
    else
    {
        ll balance=0,start=0,end=0,count=0,c1=0,c2=0;
        bool ok=true;
        for(ll i=0;i<q;i++)
        {
            if(s[i]=='(')
            {
                c1++;
                balance++;
                if(balance==0)count+=i-start+1;

            }
            else
            {
                c2++;
                balance--;
                if(balance==-1)start=i,ok=false;
                // cout<<start<<" ";
            }

        }
        if(c1==c2)
        cout<<count;
    else
        cout<<"-1";

    }

}
int main(){

    //#ifndef ONLINE_JUDGE
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //#endif
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;

  solve();
}


#include<iostream>
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll read(){
	ll x=0,w=1;
	char ch=0;
	while(ch<'0'||ch>'9'){
		if(ch=='-')
			w=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return w*x;
}

int main(){
	int len;
	cin>>len;//长度
	string ptr;
	cin>>ptr;
	int num1=0,num2=0;
	for(int i=0;i<ptr.length();i++){
		if(ptr[i]=='('){
			num1++;
		}else{
			num2++;
		}
	}
	if(num1!=num2){//不可能了   '('  和')'的数量不相等相等
		cout<<-1<<endl;
	}else{
		int a1=0,a2=0;
		int ans=0;
		for(int i=0;i<ptr.length();i=i+2){
			if(ptr[i]=='('&&ptr[i+1]==')'){
			}else{
				if(ptr[i]=='('){
					a1++;
				}else{
					a2++;
				}
				if(ptr[i+1]=='('){
					a1++;
				}else{
					a2++;
				}
				if(a1==a2&&a1!=0){
					ans=ans+a1*2;
					a1=0,a2=0;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<stack>
typedef  long long ll;
using namespace std;
int main(){
	ll n;
	cin>>n;
	string ptr;
	cin>>ptr;
	int num0=0,num1=0;
	for(int i=0;i<ptr.length();i++){
		if(ptr[i]=='('){
			num0++;
		}else{
			num1++;
		}
	}
	if(num0!=num1){
		cout<<-1<<endl;
	}else{//表示一定可以
		int a1=0 /*(*/,a2=0 /*)*/;//栈中两者的数量
		stack<char> st;
		while(!st.empty()){
			st.pop();
		}
		ll ans=0;
		for(int i=0;i<ptr.length();i++){
			if(!st.empty()){//如果不为空
				if(st.top()=='('&&ptr[i]==')'){//两者成对这又分为另一种情况
					if(a1-1!=a2){//要执行入栈
						st.push(ptr[i]);
						if(ptr[i]=='('){
							a1++;
						}else{
							a2++;
						}
					}else{//这可以配对成功
						st.pop();
						a1--;
						if(a1==a2&&a2!=0){
							ans=ans+a1*2;
							a1=0,a2=0;
							while(!st.empty()){
								st.pop();
							}
						}
					}
				}else{//两者不成对
					st.push(ptr[i]);//入栈
					if(ptr[i]=='('){
						a1++;
					}else{
						a2++;
					}
					if(a1==a2){//表明栈中两种括号的数量相等，可以组成正确序列
						//先判断他原先里面是否成对
						stack<char> st1;
						while(!st.empty()){
							if(!st1.empty()){
								if(st1.top()==')'&&st.top()=='('){
									st1.pop();
									st.pop();
								}else{
									st1.push(st.top());
									st.pop();
								}
							}else{
								st1.push(st.top());
								st.pop();
							}
						}
						if(st1.size()==0){//配对成功  不需要排序
							a1=0,a2=0;
							while(!st.empty()){
								st.pop();
							} //栈清空
						}else{//需要排序
							ans=ans+a1*2;
							a1=0,a2=0;
							while(!st.empty()){
								st.pop();
							} //栈清空
						}
					}
				}
			}else{//如果为空
				st.push(ptr[i]);
				if(ptr[i]=='('){
					a1++;
				}else{
					a2++;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<stack>
typedef  long long ll;
using namespace std;
int main(){
	ll n;
	cin>>n;
	string ptr;
	cin>>ptr;
	int num0=0,num1=0;
	for(int i=0;i<ptr.length();i++){
		if(ptr[i]=='('){
			num0++;
		}else{
			num1++;
		}
	}
	if(num0!=num1){
		cout<<-1<<endl;
	}else{//表示一定可以
		int a1=0/*(*/,a2=0/*)*/;//栈中两者的数量
		stack<char> st;
		while(!st.empty()){
			st.pop();
		}
		ll ans=0;
		for(int i=0;i<ptr.length();i++){
			if(!st.empty()){//如果不为空
				if(st.top()=='('&&ptr[i]==')'){//两者成对
					st.pop();
					//栈中的  '(' 减少一个
					a1--;
				}else{//两者不成对
					st.push(ptr[i]);//入栈
					if(ptr[i]=='('){
						a1++;
					}else{
						a2++;
					}
					if(a1==a2){
						ans=ans+a1*2;
						a1=0,a2=0;
						while(!st.empty()){
							st.pop();
						} //栈清空
					}
				}
			}else{//如果为空
				st.push(ptr[i]);
				if(ptr[i]=='('){
					a1++;
				}else{
					a2++;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<stack>
typedef  long long ll;
using namespace std;
int main(){
	ll n;
	cin>>n;
	string ptr;
	cin>>ptr;
	int num0=0,num1=0;
	for(int i=0;i<ptr.length();i++){
		if(ptr[i]=='('){
			num0++;
		}else{
			num1++;
		}
	}
	if(num0!=num1){
		cout<<-1<<endl;
	}else{//表示一定可以
		int a1=0/*(*/,a2=0/*)*/;//栈中两者的数量
		stack<char> st;
		while(!st.empty()){
			st.pop();
		}
		ll ans=0;
		for(int i=0;i<ptr.length();i++){
			if(!st.empty()){//如果不为空
				if(st.top()=='('&&ptr[i]==')'){//两者成对这又分为另一种情况
					if(a1-1!=a2){//要执行入栈
						st.push(ptr[i]);
						if(ptr[i]=='('){
							a1++;
						}else{
							a2++;
						}
					}else{
						st.pop();
						a1--;
					}
				}else{//两者不成对
					st.push(ptr[i]);//入栈
					if(ptr[i]=='('){
						a1++;
					}else{
						a2++;
					}
					if(a1==a2){
						//先判断他原先里面是否成对
						string ar;
						while(!st.empty()){
							ar=ar+st.top();
							st.pop();
						}
						string e1,e2;
						for(int x=0;x<ar.length()/2;x++){
							e1=e1+ar[x];
						}
						for(int x=ar.length()/2;x<ar.length();x++){
							e2=e2+ar[x];
						}
//						cout<<e1<<" "<<e2<<endl;
						string t1=e2+e1;
//						cout<<t1<<endl;
						stack<char>st1;
						for(int x=0;x<t1.length();x++){
							if(st1.size()==0){
								st1.push(t1[x]);//入栈
							}else{
								if(st1.top()=='('&&t1[x]==')'){
									st1.pop();
								}else{
									st1.push(t1[x]);
								}
							}
						}
						if(st1.size()==0){//配对成功  不需要排序
							a1=0,a2=0;
							while(!st.empty()){
								st.pop();
							} //栈清空
						}else{
							ans=ans+a1*2;
							a1=0,a2=0;
							while(!st.empty()){
								st.pop();
							} //栈清空
						}
					}
				}
			}else{//如果为空
				st.push(ptr[i]);
				if(ptr[i]=='('){
					a1++;
				}else{
					a2++;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

/*20
)))()()()(((()((()))*/


#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int n , timer = 0 , t = -1 , a1 = 0 ; string s  ;
	cin>>n;
	cin>>s;
	//cout<<s;
	if(n%2 != 0) {
	    cout<<"-1"<<endl;
	    return 0 ;
	}
	//string p ;
	for(int i = 0 ; i < n; i++){
	    if(s[i]==')') a1++;
        else a1--;
        if(a1==0){
        if( s[i]=='(') timer = timer + i - t;
        t=i;
        }
    }
    if(a1==0){
        cout<<timer<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int n , timer = 0 , t = -1 , a1 = 0 ; string s  ;
	cin>>n;
	cin>>s;

	for(int i = 0 ; i < n; i++){
	    if(s[i]==')') a1++;
        else a1--;
        if(a1==0){
        if( s[i]=='(') timer = timer + i - t;
        t=i;
        }
    }
    if(a1==0){
        cout<<timer<<endl;
	}
		else{
	    cout<<"-1"<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	std::queue<char> symbol  ; ;
	int n ;
	char input;
	cin>>n;
	for(int i = 0 ; i<n ; i++){
	    cin>>input;
	    symbol.push(input);
	}

	int p = symbol.size(); int timer = 0;
	if(p%2 != 0) {cout<<"-1"<<endl;
	    return 0 ;
	}
	while(p){
	    auto i = symbol.front(); symbol.pop();
	    auto j = symbol.front(); symbol.pop();
	    if(((i == '(') && (j == '(')) || ((i == ')') &&(j == ')'))) timer += 2;

	    p = p -2;
	}
	cout<<timer<<endl;

	return 0;
}


#include<bits/stdc++.h>
#define endl "\n"
#define ll long long
#define sp " "
#define pi acos(-1)
using namespace std;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	#ifndef ONLINE_JUDGE
    	freopen("input.txt", "r", stdin);
    	freopen("output.txt", "w", stdout);
	#endif
	ll q=1;//cin>>q;
	while(q--){
		ll n,p(0),k(0);string s;cin>>n>>s;
		if(s[0]=='(')
			p++;
		else {
			p--;k++;
		}
		if(n & 1)
			cout<<"-1\n";
		else{
			for(int i=1;s[i];i++){
				if(p>0){
					if(s[i]=='(')
						p++;
					else
						p--;
				}
				else{
					if(s[i]==')'){
						p--;k++;
					}
					else
						p++;
				}
			}
			cout<<2*k<<endl;
		}
	}
}


#include<bits/stdc++.h>
#define endl "\n"
#define ll long long
#define sp " "
#define pi acos(-1)
using namespace std;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	#ifndef ONLINE_JUDGE
    	freopen("input.txt", "r", stdin);
    	freopen("output.txt", "w", stdout);
	#endif
	ll q=1;//cin>>q;
	while(q--){
		ll n,p(0),k(0),f(0),g(0);string s;cin>>n>>s;
		if(s[0]=='('){
			p++;f++;
		}
		else {
			p--;k++;g++;
		}
		if(n & 1)
			cout<<"-1\n";
		else{
			for(int i=1;s[i];i++){
				if(p>0){
					if(s[i]=='('){
						p++;f++;
					}
					else {
						p--;g++;
					}
				}
				else{
					if(s[i]==')'){
						p--;k++;g++;
					}
					else{
						p++;f++;
					}
				}
			}
			if(f!=g)
				cout<<"-1\n";
			else
				cout<<2*k<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int n,k(0),d,l(0);string s;cin>>n>>s;
    if(n%2==1)cout<<"-1";
    else{
        for(int i=0;i<n;i++){
            if(s[i]=='('){l++;}
            else{l--;}
        if(l<0){k++;}
        }
        if(k%2==1)cout<<"-1";
        else cout<<k;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int n,k(0),d;string s;cin>>n>>s;
    if(n%2==1)cout<<"-1";
    else{
        for(int i=0;i<n;i=i+2){
            if(s[i]==')' || s[i+1]=='('){s[i]='(';s[i+1]=')';k=k+2;}
        }
        cout<<k;
    }
}


#include <bits/stdc++.h>
using namespace std;

void solve()
{
	long long int n,i=0,j,count=0,count1=0;
	string s;
	cin>>n>>s;

	if(n%2)
	cout<<"-1"<<endl;
	else
	{
		while(1)
	{
		if(s[i]==')')
		{
			j=i;
			count=-1;
			i+=1;
			if(i>n-1)
			{
				cout<<"-1"<<endl;
				return;
			}
			while(1)
			{
				if(s[i]==')')
				--count;
				else ++count;
				++i;
				if(count==0)
				break;
			}
			count1+=i-j;
		}
		else
		{
			count=1;
			i+=1;
			if(i>n-1)
			{
				cout<<"-1"<<endl;
				return;
			}
			while(1)
			{
				if(s[i]==')')
				--count;
				else ++count;
				++i;
				if(count==0)
				break;
			}
		}
		if(i>=n)
		break;
	}
	cout<<count1<<endl;
	}
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  long long int t;

  	solve();
}


#include <bits/stdc++.h>
using namespace std;

void solve()
{
	long long int n,i=0,j,count=0,count1=0;
	string s;
	cin>>n>>s;

	if(n%2)
	cout<<"-1"<<endl;
	else
	{
		while(1)
	   {
		if(s[i]==')')
		{
			j=i;
			count=-1;
			i+=1;
			while(1)
			{
				if(s[i]==')')
				--count;
				else ++count;
				++i;
				if(count==0)
				break;
				if(i>n-1)
				{
					cout<<"-1"<<endl;
					return;
				}
			}
			count1+=i-j;
		}
		else
		{
			count=1;
			i+=1;
			while(1)
			{
				if(s[i]==')')
				--count;
				else ++count;
				++i;
				if(count==0)
				break;
				if(i>n-1)
				{
					cout<<"-1"<<endl;
					return;
				}
			}
		}
		if(i>=n)
		break;
	}
	cout<<count1<<endl;
	}
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  long long int t;

  	solve();
}


#include <bits/stdc++.h>
using namespace std;

void solve()
{
	long long int n,i=0,j,count=0,count1=0;
	string s;
	cin>>n>>s;

	if(n%2)
	cout<<"-1"<<endl;
	else
	{
		while(1)
	{
		if(s[i]==')')
		{
			j=i;
			count=-1;
			i+=1;
			while(1)
			{
				if(s[i]==')')
				--count;
				else ++count;
				++i;
				if(count==0)
				break;
			}
			count1+=i-j;
		}
		else
		{
			count=1;
			i+=1;
			while(1)
			{
				if(s[i]==')')
				--count;
				else ++count;
				++i;
				if(count==0)
				break;
			}
		}
		if(i>=n)
		break;
	}
	cout<<count1<<endl;
	}
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  long long int t;

  	solve();
}


#include <bits/stdc++.h>
using namespace std;

void solve()
{
	long long int n,i=0,j,count=0,count1=0;
	string s;
	cin>>n>>s;

	if(n%2)
	cout<<"-1"<<endl;
	else
	{
		while(1)
	   {
		if(s[i]==')')
		{
			j=i;
			count=-1;
			i+=1;
			if(i>(n-1))
			{
				cout<<"-1"<<endl;
				return;
			}
			while(1)
			{
				if(s[i]==')')
				--count;
				else ++count;
				++i;
				if(count==0)
				break;
			}
			count1+=i-j;
		}
		else
		{
			count=1;
			i+=1;
			if(i>(n-1))
			{
				cout<<"-1"<<endl;
				return;
			}
			while(1)
			{
				if(s[i]==')')
				--count;
				else ++count;
				++i;
				if(count==0)
				break;
			}
		}
		if(i>=n)
		break;
	}
	cout<<count1<<endl;
	}
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  long long int t;

  	solve();
}


#include<bits/stdc++.h>
using namespace std;
int main(void)
{
    int n,i,j,c=0;
    j=0;
    string s;
    cin>>n;
    cin>>s;
    for(i=0;i<n;i++)
    {
        if(s[i]=='(')
        j++;
        else
        {
            j--;
            if(j<0)
            c++;
        }
    }
    cout<<(j==0 ? c*2:-1)<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main(void)
{
    int n,i,j,c=0;
    j=0;
    string s;
    cin>>s;
    for(i=0;i<s.length();i++)
    {
        if(s[i]=='(')
        j++;
        else
        {
            j--;
            if(j<0)
            c++;
        }
    }
    cout<<(j==0 ? c*2:-1)<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main(void)
{
    int n,i,j,c=0;
    j=0;
    string s;
    cin>>s;
    for(i=0;i<s.length();i++)
    {
        if(s[i]=='(')
        j++;
        else
        {
            j--;
            if(j<0)
            c++;
        }
    }
    cout<<j==0 ? c*2:-1<<endl;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    if(n % 2 == 1) {
        cout << -1 <<endl;
    } else {
        int res = 0;
        for(int i = 0; i < n; i += 2) {
            if(s[i] != '(' || s[i + 1] != ')') {
                res += 2;
            }
        }

        cout << res << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    if(n % 2 == 1) {
        cout << -1 << endl;
    } else {
        int op = 0, cl = 0, res = 0, curr = 0, prev = -1;
        for(int i = 0; i < n; i++) {
            if(s[i] == '(') {
                op++;
                curr++;
            } else {
                if(op > 0) {
                    op--;
                } else {
                   cl++;
                }
                curr--;
            }

            if(curr == 0) {
                if(op != 0 || cl != 0) {
                    res += (i - prev);
                    op = cl = 0;
                }
                prev = i;
            }
        }

        if(curr == 0) {
            cout << res << endl;
        } else {
            cout << -1 << endl;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    if(n % 2 == 1) {
        cout << -1 << endl;
    } else {
        int op = 0, cl = 0, res = 0, curr = 0, prev = -1;
        for(int i = 0; i < n; i++) {
            if(s[i] == '(') {
                op++;
                curr++;
            } else {
                if(op > 0) {
                    op--;
                } else {
                   cl++;
                }
                curr--;
            }

            if(curr == 0 && (op != 0 || cl != 0)) {
                res += (i - prev);
                op = cl = 0;
                prev = i;
            }
        }

        if(curr == 0) {
            cout << res << endl;
        } else {
            cout << -1 << endl;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    if(n % 2 == 1) {
        cout << -1 <<endl;
    } else {
        int op = 0, cl = 0;

        for(char c : s) {
            if(c == '(') {
                op++;
            } else {
                if(op > 0) {
                    op--;
                } else {
                    cl++;
                }
            }
        }

        cout << 2*(op + cl) << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    if(n % 2 == 1) {
        cout << -1 <<endl;
    } else {
        int res = 0;
        for(int i = 0; i < n; i += 2) {
            if(s[i] != '(' && s[i + 1] != ')') {
                res += 2;
            }
        }

        cout << res << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    if(n % 2 == 1) {
        cout << -1 <<endl;
    } else {
        int op = 0, cl = 0, res = 0;
        for(int i = 0; i < n; i++) {
            if(s[i] == '(') {
                op++;
            } else {
                if(op > 0) {
                    op--;
                } else {
                   cl++;
                }
            }

            if(cl > 0 && op == cl) {
                res += op + cl;
                op = cl = 0;
            }
        }
        cout << res << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    if(n % 2 == 1) {
        cout << -1 << endl;
    } else {
        int op = 0, cl = 0, res = 0, curr = 0;
        for(int i = 0; i < n; i++) {
            if(s[i] == '(') {
                op++;
                curr++;
            } else {
                if(op > 0) {
                    op--;
                } else {
                   cl++;
                }
                curr--;
            }

            if(curr == 0 && (op != 0 || cl != 0)) {
                res += op + cl;
                op = cl = 0;
            }
        }
        cout << res << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;cin>>n;
    string s;cin>>s;
    int t=0,count=0;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='(')
            count++;
        else if(count==0)
        {
            count++;
            t++;
        }
        else
            count--;
    }
    if(count%2==1)
        cout<<-1<<endl;
    else
        cout<<t+count/2<<endl;
    return 0;
}