Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	a, b, c := io.NextInt(), io.NextInt(), io.NextInt()
	for i := 0; i < 61; i++ {
		for j := 0; j < 61; j++ {
			if (a+b)*i-60*j <= c && c <= (a+b)*i-60*j+a {
				io.Println(60*j + c)
				return
			}
		}
	}
	io.Println(-1)
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := make([]interface{}, 0, len(a))
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := make([]interface{}, 0, len(a))
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include<iostream>
#include<climits>
#define ll long long 
using namespace std;

int main(){
  int a,b;
  ll c,time=0;

  cin>>a>>b>>c;

  bool flag=1;
  if(a+b>c) flag=0;

  while(1){
    if(time+b>=60) flag=0;
    if(c>=INT_MAX){
      c=-1;
      break;
    }

    time+=a;
    if(time>=c) break;
    time+=b;


    if(flag==0){
      c+=60;
      flag=1;
    }

  }

  cout<<c<<endl;
  
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cmath>
using namespace std;

#define ll long long int
#define ld long long double
#define reps(i,f,n) for(int i=f;i<n;i++)
#define rep(i,n) reps(i,0,n)
#define m1 cout<<"move1"<<endl
#define m2 cout<<"move2"<<endl
#define m3 cout<<"move3"<<endl
#define m4 cout<<"move4"<<endl


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cmath>
using namespace std;

#define ll long long int
#define ld long long double
#define reps(i,f,n) for(int i=f;i<n;i++)
#define rep(i,n) reps(i,0,n)
#define m1 cout<<"move1"<<endl
#define m2 cout<<"move2"<<endl
#define m3 cout<<"move3"<<endl
#define m4 cout<<"move4"<<endl

int main(){
  int flag = 1;
  int a,b,c;
  int t=0;
  int c_ans;
  int counter =0;

  cin>>a>>b>>c;
  c_ans = c;

  while(1){
    counter++;
    if(counter >= 100000)
      {
	cout<<-1<<endl;
	break;
      }
    if( c+60*t - a <= 0 && flag == 1){
      //m1;
      cout<< c_ans + 60*t<<endl;
      break;
    }
    //else if(c + 60 * t - a <=0 && flag == 1){
    //t++;
    //flag = 0;
    //}
    else if(c + 60 * t - a >= 0 && flag == 1){
      //m2;
      c-=a;
      flag = 0;
    }
    else if(c + 60 * t - b == 0 && flag == 0){
      //m3;
      cout<<c_ans + 60 * t <<endl;
      break;
    }
    else if(c + 60 * t - b < 0 && flag == 0){
      if( 60 % ( a + b ) == 0){
	cout<<-1<<endl;
	break;
      }
      else{
	t++;
      flag = 1;
      c-=b;
      // m4;
      }
    }
    else {
      c -= b;
      flag = 1;
    }
  }

  return 0;
}

C++: #include<iostream>


#define FOR(i,a,b) for(int i = a;i < b;i++)
#define RFOR(i,a,b) for(int i = a;i >= b;i--)
#define IN cin >> 
#define OUT cout <<
#define E  << endl;

using namespace std;


int goal_time = 0;

int a, b, c;

void input() {
	IN a >> b >> c;
}

int main() {
	input();
	int memo = 1;
	if ((60 % (a + b) == 0) &&  c > a) {
		OUT - 1 E
		return 0;
	}
	bool flag = false;
	goal_time += c;
	while (true) {
		int n = memo;
		while (true){
			if ((a + b) * n <= goal_time && (a + b) * n + a >= goal_time) {
				OUT goal_time E
				flag = true;
				break;
			}
			if ((a + b) * n > goal_time) {
				memo = n;
				break;
			}
			n++;
		}
		if (flag)break;
		goal_time += 60;
	}

	return 0;
}

C++: //	created : 16/01/07
// 	autor   : Rp7rf
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>

using namespace std;
#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define vi vector<int>


int main(void){
	int a,b,c,s = 0;
	cin>>a>>b>>c;
	int i = 0;
	while(1){
		if(i == 100)break;
		//cout<<s<<" "<<c<<" "<<s+a<<" ";
		s %= 60;c%=60;
		//cout<<s<<" "<<c<<" "<<s+a<<endl;
		if(s <= c && c <= s + a && (s+a)%60){
			s += b;
			break;
		}else if(s+a > c){
			c += 60;
		}if(s+a <= c){
			s += b;
		}
		i ++;
	}
	if(i == 100)cout<<-1<<endl;
	else cout<<i*60+c<<endl;
}

C++: #include <bits/stdc++.h>

using namespace std;

long long lcm(int a, int b)
{
  return a / __gcd(a, b) * b;
}

int main()
{
  int a, b, c;
  cin >> a >> b >> c;
  
  int ans = -1;
  bool flag = true;
  int wake = 0, sleep = 0;
  for(int i = 0; i < lcm(60, a+b); i++){
    if(flag){
      if(i % 60 == c){
	ans = i;
	break;
      }
      wake++;
      if(wake-1 == a) wake = 0, flag = false;
    } else {
      sleep++;
      if(sleep-1 == b) sleep = 0, flag = true;
    }
  }
  cout << ans << endl;
  return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
 
const int INF = 10000000;



int main(){
	ll a, b, c;
	cin>>a>>b>>c;
	if(c%(a+b)<=a){
		cout<<c<<endl;
		return 0;
	}
	if((a+b)%60==0){
		cout<<-1<<endl;
		return 0;
	}
	for(int i = 0; i < 1e7; i++){
		c+=60LL;
		if(c%(a+b)<=a){
			cout<<c<<endl;
			return 0;;
		}
	}
	cout<<-1<<endl;
}