#include <iostream>
#include <string>
using namespace std;

int string2sec(const std::string& str) {
  int i = 0;
  int res = -1;
  int tmp = 0;
  int state = 0;

  while(str[i] != '\0') {
    // If we got a digit
    if(str[i] >= '0' && str[i] <= '9') {
      tmp = tmp * 10 + str[i] - '0'; // Little bug fix here
    }
    // Or if we got a colon
    else if(str[i] == ':') {
      // If we were reading the hours
      if(state == 0) {
        res = 3600 * tmp;
      }
      // Or if we were reading the minutes
      else if(state == 1) {
        res += 60 * tmp;
      }
      // Or if we were reading the seconds
      else if(state == 2) {
        res += tmp;
      }
      // Or we got an extra colon
      else {
        return -1;
      }

      state++;
      tmp = 0;
    }
    // Or we got something wrong
    else {
      return -1;
    }
    i++;
  }

  return res;
}
int main() {
	std::cout << string2sec("1:01:01") << std::endl;
	return 0;
}