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 <stdio.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>
#include <sstream>
#include <numeric>
#include <set>
#include <map>
//#include <bits/stdc++.h>

#define rep(i, j) for(int i = 0; i < j; i++)
#define all(i) i.begin(), i.end()
#define ll long long
#define bl bool
#define nn printf("\n");
#define mod 1000000007

using namespace std;

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	int a, b, c; cin >> a >> b >> c;
	int t = 1, t_ans = 0;
	int i = 1, cri = 0;
	while (1)
	{
		if (t_ans > 100) 
		{
			printf("-1\n"); 
			break;
		}
		if (t == 60){ t = 0; t_ans++; }
		t++;
		if (i == a && cri == 0){ i = 0; cri = 1; }
		else if (i == b && cri == 1){ i = 0; cri = 0; }
		else if (cri == 0 && t == c)
		{ 
			printf("%d\n", (60 * t_ans) + c); 
			break;
		}
		i++;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a, b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout <<#x<<" = "<<(x)<<endl
#define spair(p) cout <<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(i,v.size())cout<<" "<<v[i];cout<<endl
#define sset(s) cout<<#s<<":";each(i,s)cout <<" "<<i;cout<<endl

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100005;

int main()
{
    int a,b,c;
    cin >> a >> b >> c;
    if(60 % (a+b) == 0){
        if(c % (a+b) > a){
            cout << "-1\n";
            return 0;
        }
    }
    int k = 0;
    while(1){
        if((60*k+c) % (a+b) <= a){
            cout << 60*k+c << "\n";
            break;
        }
        k++;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < static_cast<int>(n); ++i)
#define REPR(i, n) for (int i = static_cast<int>(n) - 1; i >= 0; --i)
#define FOR(i, a, b) \
  for (int i = static_cast<int>(a); i < static_cast<int>(b); ++i)
#define FORR(i, a, b) \
  for (int i = static_cast<int>(b) - 1; i >= static_cast<int>(a); --i)
#define ALL(f, x, ...) \
  ([&](auto& y) { return (f)(begin(y), end(y), ##__VA_ARGS__); })(x)
auto INDEX = [](auto& x, auto& xs) { return &x - &xs[0]; };
void async_io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
const auto inf = INT_MAX / 3;
const auto pi = 2 * std::acos(0.0);
const auto eps = 1e-14;
using ll = signed long long;
using ull = unsigned long long;

signed main() {
  async_io();
  int a, b, c;
  cin >> a >> b >> c;
  // [0..a] + [a+b % 60 .. $0 + a % 60] ...
  bool seeked[60];
  for (int i = 0, j = 0;;) {
    if (seeked[i]) {
      cout << "-1";
      return 0;
    }
    if (i <= c and c <= i + a) {
      cout << 60 * j + c;
      return 0;
    }
    seeked[i] = true;
    if (i + a + b >= 60) j++;
    i = (i + a + b) % 60;
  }
}

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

#define rep(i,n) for(int i=0; i < n; i++)

int main() {
	while (true) {
		int a, b, c;
		cin >> a >> b >> c;
		bool sleep = false;
		int cnt = 0;
		int ans = -1;
		for (int t = 1; t <= 1000000; t++) {
			if (t % 60 == c and !sleep) {
				ans = t;
				break;
			}
			cnt++;
			if (sleep and cnt == b) {
				sleep = false;
				cnt = 0;
			}
			else if (not sleep and cnt == a) {
				sleep = true;
				cnt = 0;
			}
		}
		cout << ans << endl;
		break;
	}
}

C++: #include<cstdio>
using namespace std;
int main(){
  int a,b,c,z=0;
  
  scanf("%d %d %d",&a,&b,&c);
  if( (a+b)%60 == 0){
    if(a>=c) printf("%d\n",c);
    else printf("-1\n");
  }
  
  else {
    while(1){
      
      if(z<=c && a>=c){
	printf("%d\n",c);
	break;
      }
      else if(c < z){
	c +=60;
      }
      if(c > a){
	z += a+b;
	a += z;
      }
    }
  }
  return (0);
}

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>
#include <sstream>
#include <numeric>
#include <set>
#include <map>
//#include <bits/stdc++.h>

#define rep(i, j) for(int i = 0; i < j; i++)
#define all(i) i.begin(), i.end()
#define ll long long
#define bl bool
#define nn printf("\n");
#define mod 1000000007

using namespace std;

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	int a, b, c; cin >> a >> b >> c;
	int t = 1, t_ans = 0;
	int i = 1, cri = 0;
	while (1)
	{
		if (a + b == 60 && t_ans > 5) 
		{
			printf("-1\n"); 
			break;
		}
		if (t == 60){ t = 0; t_ans++; }
		t++;
		if (i == a && cri == 0){ i = 0; cri = 1; }
		else if (i == b && cri == 1){ i = 0; cri = 0; }
		else if (i <= b && cri == 0 && t == c)
		{ 
			printf("%d\n", (60 * t_ans) + c); 
			break;
		}
		i++;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int main() {
	int a, b, c; scanf("%d%d%d", &a, &b, &c);
	set<int>se;
	for (int i = 0;; i++) {
		int j = (60 * i + c) % (a + b);
		if (j <= a) {
			printf("%d\n", 60 * i + c);
			break;
		}
		else {
			if (se.find(j) != se.end()) {
				puts("-1");
				break;
			}
			se.insert(j);
		}
	}
}

C++: #include <cstdio>
int main(){
int a,b,c,d,i;scanf("%d%d%d",&a,&b,&c);i=c-60;d=a+b;
while((i+=60)<3600)
if(0<=i%d&&i%d<=a)
break;
printf("%d\n",i>3599?-1:i);return 0;}

C++: #include<iostream>
using namespace std;
int main(){
    int n,m,b,c,d;
    bool a=false;
    cin>>n>>m>>b;c=b;
    for(int i=0;i<358000000;i++){
        if(c%(n+m)<=n && a==false){
            a=true;
            d=i;
        }
        c+=60;
    }
    if(a==false){
        cout<<"-1"<<endl;
    }
    else{
        c=b+d*60;
        cout<<c<<endl;
    }
    return 0;
}