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 <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS; typedef istringstream ISS;
typedef long long LL;
typedef pair<int, int> PII;  typedef pair<LL, LL> PLL;
typedef vector<int>    VI;   typedef vector<VI>   VVI;   typedef vector<VVI>   VVVI;
typedef vector<LL>     VLL;  typedef vector<VLL>  VVLL;  typedef vector<VVLL>  VVVLL;
typedef vector<double> VD;   typedef vector<VD>   VVD;   typedef vector<VVD>   VVVD;
typedef vector<string> VS;   typedef vector<VS>   VVS;   typedef vector<VVS>   VVVS;
typedef vector<bool>   VB;   typedef vector<VB>   VVB;   typedef vector<VVB>   VVVB;
typedef vector<PII>    VPII; typedef vector<VPII> VVPII; typedef vector<VVPII> VVVPII;
typedef vector<PLL>    VPLL; typedef vector<VPLL> VVPLL; typedef vector<VVPLL> VVVPLL;

typedef unsigned int  UI;
typedef vector<UI>    VUI;
typedef vector<VUI>   VVUI;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back 
#define ALL(x)  (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define RANGEBOX(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define RANGE(x, l, r) ((l) <= (x) && (x) <= (r))
#define rep(i, N)  for (int i = 0; i < (int)(N); i++)
#define rrep(i, N) for (int i = N - 1; i >= 0; i--)
#define REP(i, init, N)  for (int i = (init); i < (int)(N); i++)
#define RREP(i, N, last) for (int i = (init - 1); i >= last; i--)
#define MAXUD(orig, target) orig = max(orig, target)
#define MINUD(orig, target) orig = min(orig, target)
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

template < typename T > inline T fromString(const string &s) { T res; ISS iss(s); iss >> res; return res; };
template < typename T > inline string toString(const T &a)   { OSS oss; oss << a; return oss.str(); };

template<typename T=int> inline void dump(vector<T> vs, bool ent=false) { rep(i, vs.size()) cout << vs[i] << (i+1==vs.size() ? '\n' : ' '); if (ent) cout << endl; }
template<typename T = int> inline void dump(vector<vector<T>> vs, bool ent = false) { rep(i, vs.size()) dump<T>(vs[i]); if (ent) cout << endl; }

const int    INF  = 0x3f3f3f3f;
const LL     INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int    DX[] = {1,  0, -1, 0};
const int    DY[] = {0, -1,  0, 1};
const double EPS  = 1e-12;
// const double PI   = acos(-1.0);
// lambda: [](T1 x)->T2{return y;}
// simple lambda: [](T x){return x;}

int solve(int a, int b, int c) {
    int t = 0;
    rep(i, 100) {
        rep(j, a) {
            ++t;
            if (t % 60 == c) return t;
        }
        rep(j, b) {
            ++t;
        }
    }

    return -1;
}

int main(void) {
    int a, b, c;
    cin >> a >> b >> c;

    cout << solve(a, b, c) << endl;

    return 0;
}

C++: #include <iostream>
using namespace std;

int GCD(int m, int n)
{
	if (n == 0)return m;
	return GCD(n, m % n);
}

int main()
{
	int a, b, c, gcd, n, res = -1;
	cin >> a >> b >> c;
	gcd = GCD(60,c);
	n = 60/gcd;
	for (int i = 0; i < n; i++) {
		if ((c + 60 * i) % (a + b) >= 0 && (c + 60 * i) % (a + b) <= a) {
			res = c + 60 * i;
			break;
		}
	}
	cout << res << endl;
	return 0;
}

C++: #include<cstdio>
#include<functional>
#include<algorithm>
#include<cstring>
using namespace std;
int main(void)
{
	int i,j,p,a,b,c,pp;
	int flg[60];
	scanf("%d %d %d",&a,&b,&c);
	for(i=0;i<60;i++)	flg[i]=0;
	p=0;
	while(1)	{
		pp=p%60;
		if(pp==c)	{
			break;
		}
		for(i=1;i<=a;i++)	{
			p++;
			pp=p%60;
			if(pp==c)	{
				break;
			}
		}
		if(pp==c)	break;
		if(flg[pp]==1)	{
			p=-1;
			break;
		}
		else	{
			flg[pp]=1;
		}
		p+=b;
	}
	printf("%d\n",p);	
	return 0;
}

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

int a, b, c, ans = -1, now = 0;

int main() {
  cin >> a >> b >> c;
  while(ans == -1) {
    // cout << a + now << " " << now << endl;
    if((now + a) % 60 == 0) {
      if(now % 60 <= c) ans = (now / 60) * 60 + c;
    }
    else {
      if((now % 60 < (now + a) % 60) && now % 60 <= c && c <= (now + a) % 60)
        ans = (now / 60) * 60 + c;
      if((now + a) % 60 < now % 60) {
        if(now % 60 <= c) ans = (now / 60) * 60 + c;
        if(c <= (now + a) % 60) ans = ((now + a) / 60) * 60 + c;
      }
    }
    now += a + b;
    if(now % 60 == 0) break;
  }
  cout << ans << endl;
  return 0;
}

C++: #include <iostream>
using namespace std;

int main() {

  long long int a, b, c;
  cin >> a >> b >> c;

  if ( ( 60 % ( a + b ) ) == 0 ) {

    if ( ( c % ( a + b ) ) <= a ) {
      cout << c << endl;
    }else {
      cout << -1 << endl;
    }

  }else {

    for ( long long int t = 0;; t++ ) {

      if ( ( ( c + 60 * t ) % ( a + b ) ) <= a ) {
	cout << c + 60 * t << endl;
	break;
      }

    }

  }

  return 0;

}

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

vector<vector<bool>> list(1000);

bool check(){
	bool p = true;
	for(int i=0; i < list.size(); i++){
		for(int j=i+1; j < list.size(); j++){
			p = true;
			for(int k = 0; k < list[i].size(); k++) if(list[i] != list[j]){p = false; break;}
			if(p){cout << -1 << endl; return true;}
		}
	}
	return false;
}

int main(){
	int a, b, c;
	cin >> a >> b >> c;

	int n = 0;
	list[0].push_back(20);
	while(true){
		for(int i=0; i< a; i++){
			if(list[n].size() < 60) list[n].push_back(true);
			else {
				if(list[n][c]){
					cout << n*60+c << endl; return 0;
				}
				n++;
				list[n].push_back(true);
				list[n].push_back(true);
			}
		}
		if(c <= list[n].size() && list[n][c]){
			cout << (n == 0 ? 0 : (n-1))*60+c << endl; return 0;
		}

		if(1 < n && check()) return 0;

		for(int i=0; i< b; i++){
			if(list[n].size() < 60) list[n].push_back(false);
			else {
				n++;
				list[n].push_back(true);
				list[n].push_back(false);
			}
		}
	}
}