$n + x\mod y = 0.$ Given $n$ and $y,$ how do I find $x?$ I'm trying to find the difference between a time in milliseconds, and the top of the next minute. 
Given an epoch time of $1502471765429$ I'd want to find out how long it is until $1502471820000$.
Here's what I've been using thus far:
$X \ \text{mod} \ Y \equiv R$
$(X + (Y \ \text{mod} \ R)) \ \text{mod} \ Y \equiv 0$  
So:
$9 \ \text{mod} \ 10 \equiv 9$
$ 10 \ \text{mod} \ 9 \equiv 1$
$9 + 1 = 10$
$10 \ \text{mod} \ 10 \equiv 0$
But when I try it with real timestamps, it doesn't add up:
$1502471765429 \ \text{mod} \ 60000 \equiv 5429$
$60000 \ \text{mod} \ 5429 \equiv 281$
$1502471765429 + 281 = 1502471765710$
$1502471765710 \ \text{mod} \ 60000 \equiv 5710$
I was expecting a result of $54571$. What am I doing wrong here? 
 A: Why so complicated? You have $x=-n \bmod y. $ In your case $x=-1502471765429 \bmod 60000
= - (1502471765429 \bmod 60000) = 60000-5429 =54571$ 
A: In regards to your question in the title:
Given $n,y,$ and $0\equiv(n+x) \ \text{mod} \ y$, determine $x$.

Modular Arithmetic Approach: 
Well $0\equiv(n+x) \ \text{mod} \ y$
$\implies y|(n+x) $
$\implies (n+x)=ky, \ \ k\in \mathbb{Z}$. 
Therefore $x=ky-n$ so $x \in \{ -n, \ y-n, \ -y-n, \ 2y-n, \ -2y-n, \ \dots \}$. That is, this entire solution set contains all valid
  solutions for the equation. Plugging in $ky-n$ into our original
  expression also confirms our result:
$\bigg( n+ (ky-n)\bigg) \ \text{mod} \ y = (ky) \ \text{mod} \ y \equiv 0 \qquad \forall k \in \mathbb{Z}$

 

Group Theory Approach:
Assuming $0 \equiv (n+x) \ \text{mod} \ y$, we would have within
  $(\mathbb{Z}/y\mathbb{Z}):$
$n+x=e \implies (n^{-1}+n)+x=n^{-1}+e \implies x = n^{-1} \in [-n]_y$ 
thus $x=-n+ky \quad \forall k \in \mathbb{Z}$, as before.

A: That should rather be $\;\;\displaystyle 60000 \cdot \left( 1 + \frac{x - \left(x \bmod 60000\right)}{60000} \right) - x\,$.
If you define $\,\displaystyle a \operatorname{div} b= \frac{a - a \bmod b}{b}\,$ the above reduces to $\,60000 \cdot (1 + x \operatorname{div} 60000) - x\,$.
In C notation $\,\operatorname{div}\,$ is the integer division, so that would be 60000(1 + x / 60000) - x.
A: 1) How do we solve:
$k \text{ in minutes} \le x \text { in microseconds } < (k+1) \text { in minutes}$
We want to know what is $(k+1) \text { in minutes} - x \text{ in microseconds}$
$k*6000 \le x  < (k+1)*6000$
$x = k*6000 + x\%6000$ where $a \%b = r =$ the positive remainder dividing $a$ by $b$.  
(The notation $a \mod b = r$ is not actually correct as $a \mod b$ is not a single number.  $r \equiv a\mod b$ does indeed mean that $r = a - kb$ for some integer $k$ but it doesn't mean that $0\le r < b$.   $k$ may be any integer and $r \equiv a \mod b$ means that $r$ may be one of many possible number of the form $r = a \pm kb$.  So I will use the notation $a \%b$ to mean specifically the smallest non-negative such option.) 
So we simply want $y = (k+1)*6000 - x = (k+1)*6000 - (k*6000 + x\% 6000) = 6000 - x\%6000$.
That's all.
2) What did you do wrong.
Basically you assumed $R = X\%Y > \frac Y2$.  
You are correct that $X + Y\%R \equiv \text { top of next minute} \mod R$ but that doesn't mean $X + Y\%R = \text { top of next minute}$.
It means $x + y\%R + j*R = \text {top of next minute}$ for some integer $j$.  If $R > \frac Y2$ then $j = 0$ but otherwise ... not.
Which is good reason why we shouldn't treat $a \mod b$ as though it were a single specific number.
