Go: package main

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

func main() {
	io := NewIo()
	defer io.Flush()
	S, T, D := io.NextInt(), io.NextInt(), io.NextInt()
	w := make([]int, D)
	sum, min := 0, 0
	for i := 0; i < D; i++ {
		w[i] = io.NextInt()
		sum += w[i]
		min = intMin(sum, min)
		if S+sum <= T {
			io.Println(i + 1)
			return
		}
	}
	if sum >= 0 {
		io.Println(-1)
		return
	}
	ans := 0
	for ; S+min > T; ans += D {
		S += sum
	}
	for i := 0; i < D; i++ {
		S += w[i]
		if S <= T {
			io.Println(ans + i + 1)
			return
		}
	}
}

func intMin(a, b int) int {
	if a < b {
		return a
	}
	return b
}

// 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 := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	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>

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define EPS (1e-8)
#define equals(a,b) (fabs((a)-(b))<EPS)

using namespace std;

typedef long long ll;

const bool debug = 0;

bool LT(double a,double b) { return !equals(a,b) && a < b; }
bool LTE(double a,double b) { return equals(a,b) || a < b; }

#define MAX 100101
ll S,T,D,w[MAX];

ll fin(ll s,ll t) {
  rep(i,D) {
    s += w[i];
    if( s <= t ) return i + 1;
  }
  return -1;
}

bool check(ll x, ll delta, ll delta_max) {
  assert( x );
  return S + x * delta > T && S + ( x - 1 ) * delta + delta_max > T ;
}

ll bf() {
  ll cnt = 0;
  ll s = S, t = T;
  int  i = 0;
  while( s > t ) {
    s += w[i%D];
    ++cnt;
    ++i;
  }
  return cnt;
}

void compute() {
  {
    assert( S > T );
    ll tmp = fin(S,T);
    if( tmp != -1 ) {
      cout << tmp << endl;
      return;
    }
  }
  //cout << bf() << endl;
  ll delta = 0, delta_max = 0;
  rep(i,D) {
    delta += w[i];
    delta_max = min(delta_max,delta);
  }
  if( debug ) {
    cout << "delta = " << delta << ", delta_max = " << delta_max << endl;
  }

  if( delta >= 0LL ) { puts("-1"); return; }
  ll L = 0, R = 1000000LL;
  while( R - L ){
    ll M = ( L + R ) / 2LL;
    if( check(M,delta,delta_max) ) L = M + 1LL;
    else                 R = M;  
  }
  ll x = L - 1LL;
  if( debug ) {
    cout << "x = " << x << endl;
  }
  assert( x > 0LL );
  ll ans = x * D + fin( S + x * delta, T );
  
  //assert( bf() == ans );
  cout << ans << endl;
}

int main() {
  cin >> S >> T >> D;
  rep(i,D) cin >> w[i];
  compute();
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
//constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	int kkt = 1;
	while (kkt--) {
		ll S, T, D; scanf("%lld %lld %lld", &S, &T, &D); 
		vector<ll> w(D); for (int i = 0; i < D; i++) cin >> w[i];
		vector<ll> sum(D + 1); for (int i = 0; i < D; i++) sum[i + 1] = sum[i] + w[i];
		if (sum.back() >= 0) {
			for (int i = 0; i < D; i++) {
				S += w[i];
				if (S <= T) {
					cout << i + 1 << endl;
					return 0;
				}
			}
			puts("-1");
			return 0;
		}
		else {
			ll mn = *min_element(sum.begin(), sum.end());
			ll cnt = max(((S - T) + mn + sum.back()) / llabs(sum.back()), 0LL);
			S += sum.back() * cnt;
			ll res = cnt * D;
			for (int i = 0; i < D * 10; i++) {
				S += w[i % D];
				res++;
				if (S <= T) {
					cout << res << endl;
					return 0;
				}
			}
			//cout << S << endl;
		}
	}
	return 0;

}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
  ll s,t,d,w[100000],c=0;
  cin>>s>>t>>d;
  s-=t;
  for(int i=0;i<d;i++)cin>>w[i],c+=w[i];
  ll ans=0,tp=s;
  for(int i=0;tp>0&&i<d;i++)tp+=w[i],ans++;
  if(tp>0){
    if(c<0){
      c=-c;
      ans=(s-c)/c*d;
      s-=(s-c)/c*c;
      for(int i=0;s>0;i++)s+=w[i%d],ans++;
    }
    else ans=-1;
  }
  cout<<ans<<endl;
  return 0;
}

C++: #define  _CRT_SECURE_NO_WARNINGS
#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define seg_size 262144*2LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

unsigned long xor128() {
	static unsigned long x = 123456789, y = 362436069, z = 521288629, w = time(NULL);
	unsigned long t = (x ^ (x << 11));
	x = y; y = z; z = w;
	return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

void init() {
	iostream::sync_with_stdio(false);
	cout << fixed << setprecision(20);
}

#define int ll

void solve(){
	int s, t, d;
	cin >> s >> t >> d;
	int req_loss = s - t;

	vector<int> inputs;
	int summing = 0;
	int going = 0;
	REP(i, d) {
		int a;
		cin >> a;
		a *= -1LL;
		summing += a;
		going = min(going, summing);
		inputs.push_back(a);
	}
	int ans = 0;
	REP(i, 30LL * d) {
		ans++;
		req_loss -= inputs[i % d];
		if (req_loss <= 0) {
			cout << ans << endl;
			return;
		}
	}
	ll bot = 1e5;
	ll top = -1;
	while (bot - top > 1) {
		ll mid = (top + bot) / 2LL;
		int now_val = req_loss - mid * summing;
		REP(j, d) {
			now_val -= inputs[j];
			if (now_val <= 0) break;
		}
		if (now_val <= 0) {
			bot = mid;
		}
		else {
			top = mid;
		}
	}
	ans += bot * d;
	req_loss -= bot * summing;
	REP(i, 30LL * d) {
		ans++;
		req_loss -= inputs[i % d];
		if (req_loss <= 0) {
			cout << ans << endl;
			return;
		}
	}
	cout << -1 << endl;
}

#undef int
int main() {
	init();
	solve();
}